Articles on Trending Technologies

Technical articles with clear explanations and examples

Updating a MongoDB document and adding new keys only in the first document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 964 Views

To update a MongoDB document and add new keys only to the first document in a collection, use the update() method with an empty query filter and $set operator. This targets the first document that matches the query criteria. Syntax db.collection.update( {}, { $set: { "newField": "value" } }, { upsert: true } ); Sample Data db.demo162.insertMany([ { "StudentName": "Chris" }, { "StudentName": "Bob" }, { "StudentName": "David" } ...

Read More

Search for multiple documents in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 243 Views

To search for multiple documents in MongoDB, use the $in operator to match documents where a field value equals any value in a specified array. This allows you to retrieve multiple documents with a single query instead of running separate queries for each value. Syntax db.collection.find({ "fieldName": { $in: [value1, value2, value3, ...] } }); Sample Data Let us create a collection with sample client documents ? db.demo161.insertMany([ {"ClientId": 101, "ClientName": "Chris"}, {"ClientId": 102, "ClientName": "David"}, ...

Read More

GroupBy Date in MongoDB to count duplicate date records

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 312 Views

To count duplicate date records in MongoDB, use the aggregate() method with $group to group documents by date and count occurrences. Syntax db.collection.aggregate([ { $group: { _id: "$dateField", count: { $sum: 1 } } }, { $match: ...

Read More

Get execution stats in MongoDB for a collection

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 453 Views

To get execution stats in MongoDB for a collection, use the explain() method with "executionStats" parameter. This provides detailed performance information about query execution including execution time and documents examined. Syntax db.collection.find(query).explain("executionStats"); Sample Data db.demo157.insertMany([ {"Status": "Active"}, {"Status": "InActive"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e354fdffdf09dd6d08539fc"), ObjectId("5e354fe3fdf09dd6d08539fd") ] } Display all documents ...

Read More

How to sort by the difference in array contents in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

To sort by the difference in array contents, use MongoDB's aggregation pipeline with $project to calculate differences and $sort to order results. This is useful when comparing values between array elements. Syntax db.collection.aggregate([ { $project: { "fieldName": "$fieldName", "difference": { $subtract: [ { $arrayElemAt: ["$arrayField", index1] }, ...

Read More

Query a nested field within an array with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 374 Views

To query a nested field within an array in MongoDB, use the $elemMatch operator to match documents where at least one array element satisfies all specified query criteria. Syntax db.collection.find({ "arrayField": { "$elemMatch": { "nestedField": "value" } } }); Sample Data Let us create a collection with documents containing nested arrays ? ...

Read More

MongoDB query to update an array using FindAndUpdate()?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 426 Views

To update an array in MongoDB, use the findAndModify() method combined with the $ positional operator to target specific array elements. This method updates the document and returns either the original or updated version. Syntax db.collection.findAndModify({ query: { "arrayField": "matchValue" }, update: { $set: { "arrayField.$": "newValue" } } }); Sample Data db.demo152.insertMany([ {"id": 102, "Name": ["Chris", "David"], "Score": 45}, {"id": 103, "Name": ["Mike", "Carol"], "Score": 65} ]); { "acknowledged": ...

Read More

MongoDB projection result as an array of selected items?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

In MongoDB, projection results as an array of selected items can be achieved using the distinct() method. This method returns an array of distinct values for a specified field from documents that match the given query criteria. Syntax db.collection.distinct(field, query) Where field is the field to return distinct values from, and query is the optional filter criteria. Sample Data Let us create a collection with documents: db.demo151.insertMany([ {"ListOfNames": ["Chris", "David", "Mike"]}, {"ListOfNames": ["Mike", "Bob"]}, {"ListOfNames": ["John", "David", "Chris"]} ]); ...

Read More

Conditional update depending on field matched in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

To perform a conditional update in MongoDB, use the update() method with a query condition that matches specific field values, then apply $set to modify the target field. Only documents matching the condition will be updated. Syntax db.collection.update( { "fieldName": "matchValue" }, { $set: { "targetField": "newValue" } } ); Sample Data db.demo150.insertMany([ { "StudentId": 101, "StudentName": "Chris", "StudentMarks": 35 }, { "StudentId": 102, "StudentName": "Chris", "StudentMarks": 55 }, { "StudentId": 103, "StudentName": ...

Read More

MongoDB $elemMatch to match document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 397 Views

The $elemMatch operator in MongoDB matches documents that contain an array field with at least one element matching all specified query criteria. It's particularly useful when working with arrays of embedded documents. Syntax db.collection.find( { arrayField: { $elemMatch: { field1: value1, field2: value2 } } } ); Sample Data Let us create a collection with documents − db.demo313.insertMany([ { "_id": 100, "details": [{ "Name": "Chris", "Age": 24 }] }, { "_id": 101, "details": [{ "Name": "David", "Age": 22 }] }, ...

Read More
Showing 23191–23200 of 61,297 articles
Advertisements