MongoDB Articles

Page 13 of 111

Perform multiple updates with bulk operations and update elements in an array in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 370 Views

To perform multiple updates with bulk operations and update elements in an array in MongoDB, use initializeOrderedBulkOp(). It initializes and returns a new Bulk() operations builder for a collection. The builder constructs an ordered list of write operations that MongoDB executes in bulk. Syntax var bulk = db.collection.initializeOrderedBulkOp(); bulk.find({query}).updateOne({$set: {field: value}}); bulk.execute(); Sample Data Let us create a collection with documents: db.demo550.insertOne({ "Name": "Chris", "details": [ {"Marks": 49, "Result": "fail"}, ...

Read More

MongoDB concurrent update with sub collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 548 Views

To perform concurrent updates with sub collections in MongoDB, use the update() method combined with the $push operator and dot notation to reach nested fields within embedded documents and arrays. Syntax db.collection.update( { "field": "value" }, { $push: { "parentField.subField.arrayField": newValue } } ); Create Sample Data Let us create a collection with documents ? db.demo547.insertOne({ Name: "Chris", Test: { "FirstTest": { ...

Read More

Using $redact in MongoDB aggregate?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 705 Views

The $redact stage in MongoDB aggregation pipeline restricts document contents based on information stored within the documents themselves. It uses conditional logic to decide whether to keep, prune, or descend into document fields. Syntax db.collection.aggregate([ { $redact: { $cond: { if: , ...

Read More

Implement match and project in MongoDB aggregate

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

The $match operator filters documents to pass only those that match specified conditions to the next pipeline stage. The $project operator reshapes documents by including, excluding, or adding new fields before passing them to the next stage. Syntax db.collection.aggregate([ { $match: { field: value } }, { $project: { field1: 1, field2: 0, newField: "$existingField" } } ]); Sample Data Let us create a collection with student documents ? db.demo545.insertMany([ { "Name": ...

Read More

How to print NumberLong value in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 744 Views

In MongoDB, the NumberLong() wrapper handles 64-bit integers that exceed JavaScript's safe integer range. To print NumberLong values, use the toString() method or direct printing to display the value properly. Syntax var variableName = NumberLong("longNumber"); variableName.toString(); // Or direct printing print(variableName); Example 1: Using toString() Method Create a NumberLong variable and display it using toString() ? var number = NumberLong("231231231231121231"); number.toString(); NumberLong("231231231231121231") Example 2: Another NumberLong Value Display a different NumberLong value ? var anotherNumber = NumberLong("765765765765567576"); anotherNumber.toString(); NumberLong("765765765765567576") ...

Read More

Need to aggregate by hour and $avg in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 554 Views

To aggregate data by hour and calculate the average in MongoDB, use the aggregate() method with $group stage. The $hour operator extracts the hour from a date field, and $avg calculates the average value. Syntax db.collection.aggregate([ { $group: { "_id": { "$hour": "$dateField" }, "fieldName": { "$avg": "$numericField" } } ...

Read More

Find document that matches same array elements in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 450 Views

To find a document that matches the same array elements in MongoDB, use the $all operator combined with $size. The $all operator selects documents where the array field contains all specified elements, while $size ensures the array has exactly the required number of elements. Syntax db.collection.find({ "arrayField": { $all: ["element1", "element2", "element3"], $size: numberOfElements } }); Sample Data db.demo543.insertMany([ {id: 101, subject: ...

Read More

Add a boolean field true to returned objects, when a specified value is in array. For NULLnor other values, set false.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

To add a boolean field true to returned objects when a specified value exists in an array (and false for NULL or missing values), use the $ifNull operator with $setIntersection and $size to check array membership. Syntax db.collection.aggregate([ { "$project": { "fieldName": { "$eq": [ ...

Read More

Select documents grouped by field in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 508 Views

To select documents grouped by field in MongoDB, use the $group stage in an aggregation pipeline along with $project to reshape the output. The $group stage groups documents by specified field values and accumulates data using operators like $push. Syntax db.collection.aggregate([ { "$group": { "_id": { "field1": "$field1", "field2": "$field2" }, "groupedData": { "$push": { "field3": "$field3" } } ...

Read More

MongoDB query to remove subdocument from document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 988 Views

To remove a subdocument from a document in MongoDB, use the $pull operator along with update(). The $pull operator removes all array elements that match a specified condition. Syntax db.collection.update( { "matchField": "value" }, { $pull: { "arrayField": { "field": "condition" } } } ); Create Sample Data db.demo538.insertOne({ id: 101, "details": { anotherDetails: [ { ...

Read More
Showing 121–130 of 1,106 articles
« Prev 1 11 12 13 14 15 111 Next »
Advertisements