Database Articles

Page 54 of 547

Update MongoDB variable value with variable itself?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 562 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 264 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 448 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 269 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 174 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 572 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 598 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

Implement a MongoDB $cond field in a projection pipeline based on the presence or absence of a field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 297 Views

To implement a MongoDB $cond field in a projection pipeline based on the presence or absence of a field, use $cond along with $anyElementTrue. NULL values (absence of a field) evaluate to FALSE, and empty arrays also return FALSE with $anyElementTrue. Syntax db.collection.aggregate([ { "$project": { "fieldName": { "$cond": [ ...

Read More

How can I search a collection to find a nested value in one of its documents in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 157 Views

To search for nested values in MongoDB documents, use dot notation in the find() method. This allows you to query fields within embedded objects by specifying the path to the nested field. Syntax db.collection.find({"parentField.nestedField": "value"}); Sample Data Let us first create a collection with nested documents ? db.nestedDemo.insertMany([ {"Information": {"__StudentName": "John Smith"}}, {"Information": {"__StudentName": "John Doe"}}, {"Information": {"__StudentName": "Chris Brown"}} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Find MongoDB document with array containing the maximum occurrence of a specific value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 277 Views

To find a MongoDB document with an array containing the maximum occurrence of a specific value, use the aggregation pipeline with $filter, $size, and $group operators to count occurrences and identify the document with the highest count. Syntax db.collection.aggregate([ { $project: { "arrayField": 1, "occurrenceCount": { $size: { $filter: ...

Read More
Showing 531–540 of 5,468 articles
« Prev 1 52 53 54 55 56 547 Next »
Advertisements