Query array of subdocuments in MongoDB

AmitDiwan
Updated on 15-Mar-2026 03:18:47

493 Views

To query an array of subdocuments in MongoDB, you can use various operators like $unwind in aggregation pipelines, $elemMatch for matching specific array elements, or simple dot notation for basic queries. Syntax // Using $unwind in aggregation db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": "value" } } ]); // Using $elemMatch db.collection.find({ "arrayField": { $elemMatch: { "field": "value" } } }); Sample Data db.demo499.insertOne({ "details": [ ... Read More

Retrieving group by result with arrays in MongoDB?

AmitDiwan
Updated on 15-Mar-2026 03:18:33

1K+ Views

To retrieve group by result with arrays in MongoDB, use aggregate() with $unwind and $group stages. The $addToSet operator adds values to an array unless the value is already present, preventing duplicates. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$fieldToGroupBy", resultArray: { $addToSet: "$fieldToCollect" } } ... Read More

Change date format in MongoDB

AmitDiwan
Updated on 15-Mar-2026 03:18:19

541 Views

To change date format in MongoDB, you can manipulate date strings using JavaScript variables and string methods within the MongoDB shell, or use MongoDB's date aggregation operators for more complex transformations. Syntax // JavaScript string manipulation var inputDate = "01-10-2019"; var formatDate = inputDate.split(/-|\//); var outputString = formatDate[2] + '-' + formatDate[0] + '-' + formatDate[1]; // MongoDB aggregation for date formatting db.collection.aggregate([ { $project: { formattedDate: { $dateToString: { format: "%Y-%m-%d", date: "$dateField" } } }} ]); ... Read More

How to find if element exists in document - MongoDB?

AmitDiwan
Updated on 15-Mar-2026 03:18:04

661 Views

To find if an element exists in a MongoDB document, use the $exists operator. This operator checks whether a specified field is present in the document, regardless of its value. Syntax db.collection.find({"fieldName": {$exists: true}}); db.collection.find({"fieldName": {$exists: false}}); Sample Data db.demo497.insertMany([ {"details": [{"Name": "Chris"}, {"Name": "Bob"}]}, {"details": [{"Name": "Carol"}]}, {"details": [{}]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e84b3cfb0f3fa88e22790d1"), ... Read More

Display the undefined and exact MongoDB document records

AmitDiwan
Updated on 15-Mar-2026 03:17:50

377 Views

To display undefined and exact MongoDB document records, use the forEach() method with printjson(). The forEach() callback accepts two parameters: the document and the index, allowing you to display either the actual documents or undefined values. Syntax db.collection.find({}).forEach((document, index) => { printjson(document); // Display documents printjson(index); // Display undefined }); Sample Data db.demo496.insertMany([ { "Name": "David", "CountryName": "US" }, { "Name": "John", "CountryName": "AUS" }, { "Name": "Robert", ... Read More

Update only a single MongoDB document without deleting any date

AmitDiwan
Updated on 15-Mar-2026 03:17:37

299 Views

To update only a single document in MongoDB, use the updateOne() method. This method updates the first document that matches the specified filter criteria and preserves all existing data except for the fields being updated. Syntax db.collection.updateOne( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Create Sample Data Let us create a collection with documents − db.demo495.insertMany([ {"FirstName":"Chris", "Age":19}, {"FirstName":"David", "Age":21}, {"FirstName":"Bob", "Age":26}, {"FirstName":"John", ... Read More

Update elements inside an array in MongoDB?

AmitDiwan
Updated on 15-Mar-2026 03:17:22

2K+ Views

To update elements inside an array in MongoDB, use the $set operator combined with the $ positional operator. The positional operator identifies the array element that matches the query condition and allows you to update specific fields within that element. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $set: { "arrayField.$.fieldToUpdate": "newValue" } } ); Create Sample Data db.demo494.insertOne({ "CollegeDetails": [ { ... Read More

Make nested queries in MongoDB 4 to fetch a specific document

AmitDiwan
Updated on 15-Mar-2026 03:17:09

550 Views

To make nested queries in MongoDB 4 to fetch a specific document, use dot notation to traverse through nested objects and arrays. This allows you to query deeply nested fields within documents. Syntax db.collection.find({ "parentField.childField.nestedField": "value" }); Sample Data Let us first create a collection with nested documents ? db.demo492.insertMany([ { "ProductDetails": { "StockDetails": [ ... Read More

How to match date with MongoDB $match?

AmitDiwan
Updated on 15-Mar-2026 03:16:56

4K+ Views

To match date in MongoDB, use $match operator within the aggregation pipeline. The $match stage filters documents based on date criteria using comparison operators like $gte, $lte, or $eq. Syntax db.collection.aggregate([ { $match: { "dateField": { "$operator": ISODate("YYYY-MM-DDTHH:mm:ss.sssZ") } ... Read More

MongoDB query to fetch random value using Map Reduce concept.

AmitDiwan
Updated on 15-Mar-2026 03:16:41

368 Views

To fetch random values using Map Reduce in MongoDB, use the mapReduce() method combined with Math.random() to emit documents based on a probability threshold. Syntax db.collection.mapReduce( function() { if (Math.random() < threshold) emit(this._id, this); }, function(key, values) { return values; }, { out: "outputCollection" } ); Sample Data db.demo651.insertMany([ {Value: 10}, {Value: 20}, {Value: 30}, {Value: 40}, {Value: 50}, ... Read More

Advertisements