Chandu yadav

Chandu yadav

810 Articles Published

Articles by Chandu yadav

Page 28 of 81

Get the first element in an array and return using MongoDB Aggregate?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 3K+ Views

To get the first element from an array using MongoDB aggregation, use the $arrayElemAt operator or combine $unwind with $first. The $arrayElemAt method is simpler and more efficient for this task. Syntax db.collection.aggregate([ { $project: { "fieldName": { $arrayElemAt: ["$arrayField", 0] } } } ]); Sample Data db.getFirstElementInArrayDemo.insertMany([ { ...

Read More

How to reduce MongoDB storage space after deleting large amount of data?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 522 Views

To reduce MongoDB storage space after deleting a large amount of data, you need to reclaim the freed disk space since MongoDB doesn't automatically release space after deletions. The database continues to use the same amount of disk space even after documents are removed. Syntax db.runCommand({ "compact": "collectionName" }) Or for the entire database: db.repairDatabase() Method 1: Using compact Command (Recommended) The compact command is the modern approach to reclaim space from a specific collection ? db.runCommand({ "compact": "users" }) { "ok" : 1, "bytesFreed" ...

Read More

Is it possible to sum two fields in MongoDB using the Aggregation framework?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 1K+ Views

Yes, it is possible to sum two fields in MongoDB using the $add operator within the $project stage of the aggregation framework. This creates a new computed field containing the sum of existing fields. Syntax db.collection.aggregate([ { $project: { field1: "$field1", field2: "$field2", sumField: { $add: ["$field1", "$field2"] } ...

Read More

Perform conditional upserts or updates in MongoDB

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 184 Views

In MongoDB, you can perform conditional updates that only modify a field if certain conditions are met. The $max operator is particularly useful for conditional updates - it only updates a field if the new value is greater than the current value. Syntax db.collection.update( { "field": "matchValue" }, { $max: { "fieldToUpdate": newValue } } ); Sample Data Let us create a collection with sample student score documents ? db.conditionalUpdatesDemo.insertMany([ { "_id": ...

Read More

Only insert if a value is unique in MongoDB else update

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 819 Views

To only insert if a value is unique in MongoDB (or update if it exists), use the upsert option with the update() method. When upsert: true is specified, MongoDB performs an update if the document matches the query criteria, or inserts a new document if no match is found. Syntax db.collection.update( { field: "matchValue" }, { $set: { field: "newValue" } }, { upsert: true } ); Sample Data db.onlyInsertIfValueIsUniqueDemo.insertMany([ {"StudentName": "Larry", "StudentAge": 22}, ...

Read More

Query MongoDB with length criteria?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 223 Views

To query MongoDB with length criteria, you can use the $regex operator with regular expressions to match string fields based on their character length. Syntax db.collection.find({ "fieldName": { $regex: /^.{minLength, maxLength}$/ } }); Where minLength and maxLength define the character count range. Sample Data db.queryLengthDemo.insertMany([ {"StudentFullName": "John Smith"}, {"StudentFullName": "John Doe"}, {"StudentFullName": "David Miller"}, {"StudentFullName": "Robert Taylor"}, {"StudentFullName": "Chris Williams"} ]); { ...

Read More

Access objects from the nested objects structure in MongoDB

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 761 Views

To access objects from nested objects structure in MongoDB, use dot notation to traverse through multiple levels of nested documents. This allows you to query and retrieve documents based on deeply nested field values. Syntax db.collection.find({ "parentObject.childObject.nestedField": "value" }); Create Sample Data Let us first create a collection with nested documents ? db.nestedObjectDemo.insertOne({ "Student": { "StudentDetails": { "StudentPersonalDetails": { ...

Read More

Drop all indexes from all the collections in a MongoDB database using the command line?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 301 Views

To drop all indexes from all collections in a MongoDB database, use the dropIndexes command with the forEach method to iterate through all collections. This operation removes all custom indexes while preserving the mandatory _id index. Syntax db.getCollectionNames().forEach(function(collectionName) { db.runCommand({dropIndexes: collectionName, index: "*"}); }); Example Let's first check the current database and view existing indexes before dropping them ? db Test View Current Indexes Check existing indexes in a collection ? db.indexingDemo.getIndexes(); [ ...

Read More

Search for a text in MongoDBs Double Nested Array?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 170 Views

To search for a text in MongoDB's double nested array, use dot notation to traverse through multiple array levels. The dot notation format is parentArray.childArray.fieldName to target specific fields deep within nested structures. Syntax db.collection.find({ "parentArray.childArray.fieldName": "searchValue" }); Sample Data Let us create a collection with double nested array documents ? db.doubleNestedArrayDemo.insertMany([ { "StudentId": "1000", "StudentName": "Larry", "StudentDetails": [ ...

Read More

How to get all the collections from all the MongoDB databases?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 592 Views

To get all the collections from all MongoDB databases, you need to first retrieve all databases and then iterate through each database to get their collections. Syntax // Step 1: Get all databases switchDatabaseAdmin = db.getSiblingDB("admin"); allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases; // Step 2: Iterate through databases to get collections allDatabaseName.forEach(function(databaseName) { db = db.getSiblingDB(databaseName.name); collectionName = db.getCollectionNames(); // Process collections as needed }); Method 1: Get All Database Names First, let us get all the databases using the admin ...

Read More
Showing 271–280 of 810 articles
« Prev 1 26 27 28 29 30 81 Next »
Advertisements