MongoDB Articles

Page 51 of 111

How to specify the order in which the query returns matching documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 138 Views

To specify the order in which the query returns matching documents, use cursor.sort() in MongoDB. The cursor is obtained from db.collectionName.find(). Syntax db.collection.find().sort({ field: 1 }); // Ascending order db.collection.find().sort({ field: -1 }); // Descending order Where 1 means ascending order and -1 means descending order. Sample Data Let us create a collection with documents − db.demo259.insertMany([ { "Subject": "MySQL" }, { "Subject": "Java" }, { "Subject": "MongoDB" } ]); { ...

Read More

How to pull value from array of ObjectIDs in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 736 Views

To pull value from array of ObjectIDs in MongoDB, use the $pull operator with the $in operator to match and remove specific ObjectID values from the array. Syntax db.collection.update( { /* query */ }, { $pull: { arrayField: { $in: [ObjectId("id1"), ObjectId("id2")] } } } ); Sample Data Let us create a collection with documents ? db.demo258.insertOne({ "arrayOfObjectsId": [ ObjectId("5e47a5e81627c0c63e7dba92"), ObjectId("5e47a5e51627c0c63e7dba91") ...

Read More

Set MongoDB compound index with a fixed value field

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

A MongoDB compound index with a fixed value field ensures uniqueness across multiple fields, including fields that may be missing (null) in some documents. When you create a unique compound index, MongoDB treats missing fields as null values. Syntax db.collection.createIndex( { "field1": 1, "field2": 1 }, { unique: true } ); Create Sample Data First, let's create a unique compound index on StudentName and StudentAge ? db.compoundIndexDemo.createIndex( {"StudentName": 1, "StudentAge": 1}, {unique: true} ); ...

Read More

Update MongoDB variable value with variable itself?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 555 Views

To update a MongoDB field value using its current value as part of the new value, use the $set operator with string concatenation or the $concat aggregation operator within an update pipeline. Syntax // Using aggregation pipeline (recommended) db.collection.updateMany( {}, [{ $set: { fieldName: { $concat: ["$fieldName", " additional text"] } } }] ); // Using $set with literal string (replaces with literal text) db.collection.update( {}, { $set: { fieldName: "fieldName additional text" } } ); Sample Data ...

Read More

MongoDB bulk insert for documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 257 Views

MongoDB provides multiple methods for bulk inserting documents efficiently. The most common approaches are using initializeUnorderedBulkOp() for legacy bulk operations and insertMany() for modern bulk inserts. Syntax // Legacy Bulk Operation var bulkOp = db.collection.initializeUnorderedBulkOp(); bulkOp.insert(document1); bulkOp.insert(document2); bulkOp.execute(); // Modern Approach db.collection.insertMany([document1, document2, document3]); Method 1: Using initializeUnorderedBulkOp() Create a bulk operation and insert multiple documents ? var manyDocument = db.demo255.initializeUnorderedBulkOp(); manyDocument.insert({ "Name": "Chris", "Age": 24 }); manyDocument.insert({ "Name": "Bob", "Age": 22 }); manyDocument.insert({ "Name": "David", "Age": 23 }); manyDocument.execute(); BulkWriteResult({ "writeErrors" : [ ], ...

Read More

Split a string during MongoDB aggregate

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 443 Views

To split a string during MongoDB aggregation, use the $split operator which divides a string into an array based on a delimiter. For more complex operations, you can combine it with other aggregation operators like $project and $arrayElemAt. Syntax { $split: ["$fieldName", "delimiter"] } Sample Data db.splitString.insertOne({ "StudentName": "John Smith" }); { "acknowledged": true, "insertedId": ObjectId("5e0849d925ddae1f53b62206") } Method 1: Using $split in Aggregation Pipeline Split the StudentName field and extract the last name using the ...

Read More

MongoDB query to change simple field into an object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 264 Views

To change a simple field into an object in MongoDB, use the $rename operator with a temporary field approach. This involves renaming the field to a temporary name, then renaming it back with the desired object structure using dot notation. Syntax db.collection.updateMany( {}, { $rename: { "fieldName": "tempField" } } ); db.collection.updateMany( {}, { $rename: { "tempField": "objectName.nestedField" } } ); Sample Data db.changeSimpleFieldDemo.insertOne({ "StudentMarks": 58, "StudentSubject": "MySQL" ...

Read More

MongoDB query to 'sort' and display a specific number of values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

To sort and display a specific number of values in MongoDB, use the sort() method combined with limit(). The sort() method orders documents while limit() restricts the number of results returned. Syntax db.collection.find().sort({field: 1}).limit(number); Where 1 is for ascending order and -1 is for descending order. Sample Data Let us create a collection with documents ? db.demo254.insertMany([ {"Name": "Chris"}, {"Name": "Adam"}, {"Name": "Bob"} ]); { "acknowledged": true, "insertedIds": ...

Read More

MongoDB query to fetch array values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 558 Views

To fetch specific array values in MongoDB, use the find() method along with $elemMatch operator to match documents containing specific values in nested arrays. Syntax db.collection.find({ "arrayField": { $elemMatch: { "nestedField": "matchValue" } } }); Sample Data db.fetchingArrayValuesDemo.insertMany([ { "StudentName": "David", ...

Read More

MongoDB indexes not working when executing $elemMatch?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 593 Views

When MongoDB indexes don't appear to work with $elemMatch, use the explain() method to analyze query execution plans. The key issue is often that $elemMatch requires proper field path indexing and array structure understanding. Syntax db.collection.createIndex({"field.subfield": 1}); db.collection.find({"field": {$elemMatch: {"subfield": "value"}}}).explain(); Create Sample Data First, create an index on the nested field and insert sample documents: db.workingOfIndexesDemo.createIndex( {"Information.StudentDetails.StudentName": 1}, {sparse: true, background: true} ); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, ...

Read More
Showing 501–510 of 1,106 articles
« Prev 1 49 50 51 52 53 111 Next »
Advertisements