MongoDB Articles

Page 61 of 111

Deleting specific record from an array nested within another array in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 753 Views

To delete specific record from an array nested within another array in MongoDB, use the $pull operator combined with the $ positional operator to target the correct parent array element and remove the matching nested document. Syntax db.collection.update( { "parentArray.field": "matchValue" }, { $pull: { "parentArray.$.nestedArray": { "field": "valueToDelete" } } } ); Sample Data db.demo213.insertOne({ "id": 101, "details1": [ { ...

Read More

Finding a MongoDB document through a word

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 214 Views

To find a MongoDB document through a word, use the find() method with regular expression pattern matching. The /word/i syntax performs case-insensitive search for the specified word within field values. Syntax db.collection.find({ "fieldName": /searchWord/i }); Create Sample Data db.demo212.insertMany([ {"details": [{"Name": "John Doe"}]}, {"details": [{"Name": "Chris Brown"}]}, {"details": [{"Name": "Robert doe"}]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e3e2c7603d395bdc21346ff"), ...

Read More

How do you test if two external values are equal in a MongoDB criteria object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 127 Views

To test if two external values are equal in a MongoDB criteria object, you can use the $type operator combined with a JavaScript expression that evaluates the equality condition and returns different BSON type numbers. Syntax db.collection.find({ field: "value", field: { $type: baseNumber + (value1 === value2) } }); Sample Data db.demo211.insertMany([ { id: 101, "Name": "Chris" }, { id: 102, "Name": null } ]); { "acknowledged": true, ...

Read More

Get the count of a specific value in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 459 Views

To get the count of a specific value in MongoDB, use the aggregate() method with $unwind and $group stages to count occurrences of array elements or use $size with $filter for a more efficient approach. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": "targetValue" } }, { $group: { _id: "$_id", count: { $sum: 1 } } } ]); Sample Data db.demo210.insertOne({ details: [ { ClientName: "Robert" ...

Read More

MongoDB query to convert the field value and create datetime day of month during projection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 356 Views

To convert a field value and create datetime day of month during projection in MongoDB, use aggregate() with $unwind to flatten arrays and $dayOfMonth to extract the day from converted timestamps. Syntax db.collection.aggregate([ { "$unwind": "$arrayField" }, { "$project": { "fieldName": 1, "DayOfMonth": { ...

Read More

Specify a return format for data in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 367 Views

In MongoDB, you can specify a custom return format for your data using aggregation operators like $addToSet. This operator collects unique values from multiple documents and returns them as an array, eliminating duplicates. Syntax db.collection.aggregate([ { "$group": { "_id": null, "fieldName": { "$addToSet": "$sourceField" } } }, ...

Read More

MongoDB query to determine if a specific value does not exist?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 381 Views

To determine if a specific value does not exist, use the $ne (not equal) operator in MongoDB. This operator matches documents where the field value does not equal the specified value. Syntax db.collection.find({ "field": { "$ne": "value" } }) Sample Data db.demo206.insertMany([ { "ClientDetails": [ { "Name": "Chris", ...

Read More

MongoDB query to count records on the basis of matching criteria

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 680 Views

To count records on the basis of matching criteria in MongoDB, use the countDocuments() method with a query filter. This method returns the number of documents that match the specified criteria. Syntax db.collection.countDocuments({field1: value1, field2: value2}) Sample Data Let us create a collection with documents ? db.demo205.insertMany([ { "id": "101", "Name": "", "Age": "", "isActive": false ...

Read More

Display only an element found in an array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 346 Views

To display only an element found in an array in MongoDB, use aggregate() with $match, $unwind, and $project stages to filter and extract specific array elements. Syntax db.collection.aggregate([ { "$match": { "arrayField.searchField": "searchValue" }}, { "$unwind": "$arrayField" }, { "$match": { "arrayField.searchField": "searchValue" }}, { "$project": { "fieldToDisplay": "$arrayField.fieldName", "_id": 0 }} ]); Sample Data db.demo204.insertOne({ "_id": 101, "Name": "Chris", "Age": 23, ...

Read More

Query nested array by more than one condition in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 786 Views

To query nested array by more than one condition in MongoDB, use the $elemMatch operator. This operator matches documents that contain an array field with at least one element that satisfies all the specified query conditions. Syntax db.collection.find({ "arrayField": { $elemMatch: { "field1": "value1", "field2": "value2" } } ...

Read More
Showing 601–610 of 1,106 articles
« Prev 1 59 60 61 62 63 111 Next »
Advertisements