Articles on Trending Technologies

Technical articles with clear explanations and examples

How to aggregate two lists if at least one element matches in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 348 Views

To aggregate two lists if at least one element matches in MongoDB, use the aggregation pipeline with $unwind, $group, and $addToSet operators to identify common elements between arrays and group documents accordingly. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField", documentIds: { $addToSet: "$_id" }, count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } }, // Additional pipeline stages to format results ]); Sample Data db.demo456.insertMany([ ...

Read More

How do I display a list of objects based on a specific property with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 382 Views

To display a list of objects based on a specific property in MongoDB, use dot notation in the find() method to query nested object properties or array elements. Syntax db.collection.find({"object.property": value}); db.collection.find({"object.array.property": value}); Sample Data Let us create a collection with student documents ? db.demo455.insertMany([ {"Information": {"Student": [{"Name": "Chris", "Age": 22}]}}, {"Information": {"Student": [{"Name": "David", "Age": 21}]}}, {"Information": {"Student": [{"Name": "Bob", "Age": 24}]}}, {"Information": {"Student": [{"Name": "Robert", "Age": 21}]}} ]); { ...

Read More

How to continuously publish the latest N records with sorting using MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 117 Views

To publish the latest N records with sorting in MongoDB, use the sort() method combined with limit(). The sort() method orders documents, while limit() restricts the number of returned records. Syntax db.collection.find().sort({field: -1}).limit(N); Where -1 sorts in descending order, 1 for ascending order, and N is the number of records to return. Create Sample Data db.demo454.insertMany([ {"ClientName": "Chris"}, {"ClientName": "John"}, {"ClientName": "Bob"}, {"ClientName": "David"}, {"ClientName": "Mike"} ]); { ...

Read More

How to filter documents based on an array in MongoDB?

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

To filter documents based on an array in MongoDB, use the $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that satisfies all the specified query criteria. Syntax db.collection.find( { arrayField: { $elemMatch: { field1: value1, field2: value2 } } } ); Sample Data Let us create a collection with documents − db.demo453.insertMany([ { _id: 101, details: [ { Name: "David", Marks: 60 }, { Name: "Mike", Marks: 55} ] }, { ...

Read More

Get the aggregated result and find the count of repeated values in different MongoDBndocuments

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 214 Views

To count repeated values in different MongoDB documents, use the aggregate() method with the $group stage to group documents by a field and calculate the count using $sum. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } }, { $sort: { count: -1 } } ]); Sample Data db.demo452.insertMany([ { "StudentName": "John", "StudentAge": 21 }, { "StudentName": "John", "StudentAge": 22 }, { "StudentName": "John", "StudentAge": 23 }, ...

Read More

MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 920 Views

To get the mean daily average count of recorded documents in MongoDB, use the aggregate() method with $match, $group, and $project stages to filter dates, calculate totals, and compute the average based on time differences. Syntax db.collection.aggregate([ { $match: { dateField: { $lt: new ISODate() } } }, { $group: { _id: null, oldestDate: { $min: "$dateField" }, totalValue: { $sum: "$valueField" } } }, { $project: { averageValue: { $divide: ["$totalValue", { $divide: [{ $subtract: [new ISODate(), "$oldestDate"] }, 86400000] }] } } } ...

Read More

How to reach subdata in MongoDB and display a particular document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 195 Views

To reach subdata in MongoDB and display a particular document, use dot notation to access nested fields. This allows you to query specific values within nested objects and retrieve matching documents. Syntax db.collection.find({"parentField.childField.subField": "value"}); Sample Data Let us create a collection with nested documents ? db.demo450.insertMany([ {"Information": {"StudentDetails": {"StudentName": "Chris", "StudentAge": 21}}}, {"Information": {"StudentDetails": {"StudentName": "David", "StudentAge": 23}}}, {"Information": {"StudentDetails": {"StudentName": "Mike", "StudentAge": 22}}} ]); { "acknowledged": true, "insertedIds": ...

Read More

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 584 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 218 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
Showing 23021–23030 of 61,297 articles
Advertisements