Articles on Trending Technologies

Technical articles with clear explanations and examples

What is the difference between deleteOne() and findOneAndDelete() operation in MongoDB?

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

Both deleteOne() and findOneAndDelete() remove a single document from a MongoDB collection, but they differ in their return values. The deleteOne() method returns only the deletion status, while findOneAndDelete() returns the actual deleted document. Syntax // deleteOne() - returns deletion status db.collection.deleteOne(filter, options) // findOneAndDelete() - returns deleted document db.collection.findOneAndDelete(filter, options) Sample Data db.demo448.insertMany([ {"Name": "Chris", "Age": 21}, {"Name": "David", "Age": 23}, {"Name": "Bob", "Age": 22} ]); { "acknowledged": true, ...

Read More

How to aggregate two collections where a field from one collection is greater than the other in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 642 Views

To aggregate two collections where a field from one collection is greater than the other in MongoDB, use $lookup to join collections, followed by $redact and $gt to filter based on field comparison. Syntax db.collection1.aggregate([ { $lookup: { "from": "collection2", "localField": "fieldName", "foreignField": "fieldName", "as": "joinedData" }}, { $unwind: "$joinedData" }, ...

Read More

Cannot push into an array from MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 269 Views

To push into an array with MongoDB, use the $push operator. This operator adds a new element to the end of an existing array field in a document. Syntax db.collection.update( { }, { $push: { : } } ); Sample Data Let us create a collection with documents ? db.demo445.insertOne({ "ListOfFriends": ["Robert", "Mike", "Sam", "Carol", "David", "Mike"] }); { "acknowledged": true, "insertedId": ObjectId("5e78f099bbc41e36cc3caec2") } Display ...

Read More

How to improve MongoDB queries with multikey index in array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

To improve MongoDB queries with multikey index in array fields, use $elemMatch operator combined with proper compound indexes on array elements. This ensures efficient querying of nested objects within arrays. Syntax db.collection.find({ "arrayField": { $elemMatch: { "field1": "value1", "field2": "value2" } } }); Sample Data ...

Read More

MongoDB profiler output: What is the "command" operation?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 331 Views

The following operations are treated as command operations in MongoDB − 1. count 2. findAndModify 3. aggregate Command operations appear in the MongoDB profiler output with the op field set to "command" and include additional details about the specific command executed. Syntax db.collection.count(query, options); db.collection.findAndModify({query, update, options}); db.collection.aggregate(pipeline, options); Sample Data Let us create a collection with documents − db.demo443.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"} ]); { "acknowledged": true, ...

Read More

Project field in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 334 Views

To project field in MongoDB, use $project in an aggregation pipeline. The $project stage allows you to reshape documents by including, excluding, or creating new fields. Syntax db.collection.aggregate([ { $project: { "fieldName": 1, // Include field "newField": "$existingField", // Rename field ...

Read More

How to remove duplicate record in MongoDB 3.x?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 466 Views

To remove duplicate records in MongoDB, use the aggregation pipeline with $group and $addToSet operators to identify duplicates, then remove them using deleteMany(). Syntax db.collection.aggregate([ { $group: { _id: { field: "$field" }, duplicateIds: { $addToSet: "$_id" }, count: { $sum: 1 } ...

Read More

Perform simple validation in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 367 Views

For validation in MongoDB, use validator with $jsonSchema to enforce document structure and data types. Validation rules are applied when creating or updating documents. Syntax db.createCollection("collectionName", { validator: { $jsonSchema: { bsonType: "object", required: ["field1", "field2"], properties: { ...

Read More

How to merge multiple documents in MongoDB?

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

To merge multiple documents in MongoDB, use the aggregate() method with operators like $group and $push to combine data from documents that share common fields. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$groupingField", mergedArray: { $push: "$arrayField" }, sumField: { $sum: "$numericField" } }} ]); Sample Data db.demo436.insertMany([ { ...

Read More

How to append to array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

To append to array in MongoDB, use the $concatArrays operator in aggregation pipelines to combine multiple arrays into a single array. This operator concatenates arrays and returns the combined result. Syntax db.collection.aggregate([ { $project: { newField: { $concatArrays: ["$array1", "$array2", ...] } ...

Read More
Showing 23031–23040 of 61,299 articles
Advertisements