Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB query to display alternative documents with mapReduce() function and emit even field values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

To display alternative documents with the mapReduce() function and emit even field values in MongoDB, use a custom counter in the scope parameter to track document order and emit only documents at even positions. Syntax db.collection.mapReduce( function() { counter++; var id = this._id; delete this._id; if (counter % divisor != 0) ...

Read More

Calculate frequency of duplicate names from NAME field using MongoDB aggregate?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 256 Views

To calculate the frequency of duplicate names in MongoDB, use the $group stage in the aggregation pipeline to group documents by the Name field and count occurrences with $sum. Syntax db.collection.aggregate([ { $group: { "_id": "$fieldName", count: { $sum: 1 } } } ]); Create Sample Data db.demo635.insertMany([ {Name: "Chris"}, {Name: "David"}, {Name: "David"}, {Name: "Chris"}, {Name: "Bob"}, {Name: "Chris"} ]); { ...

Read More

How to update document with marks value in MongoDB for student David

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 252 Views

To update a specific document's marks value for student David in MongoDB, you can use the forEach() method to traverse the collection and find the student, then update their marks using the $set operator. Syntax db.collection.find({Name: "StudentName"}).forEach(function(doc) { db.collection.update({_id: doc._id}, {$set: {Marks: newValue}}); }); Sample Data Let us create a collection with student documents ? db.demo634.insertMany([ {Name: "Chris", Marks: 76}, {Name: "Bob", Marks: 67}, {Name: "David", Marks: 37} ]); { ...

Read More

Remove values from a matrix like document in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

To remove values from a matrix-like document in MongoDB, use the $pull operator with dot notation to target specific elements within nested arrays. This operator removes all instances of a specified value from an array field. Syntax db.collection.update( { query }, { $pull: { "arrayMatrix.index": value } } ); Sample Data Let's create a collection with a matrix-like document ? db.demo632.insertOne({ "arrayMatrix": [ [10, 20], ...

Read More

Return specific MongoDB embedded document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

To return a specific embedded document in MongoDB, use the $unwind operator twice to flatten nested arrays, followed by $match to filter the desired embedded document based on specific criteria. Syntax db.collection.aggregate([ { "$unwind": "$arrayField1" }, { "$unwind": "$arrayField1.arrayField2" }, { "$match": { "arrayField1.arrayField2.field": "value" } } ]); Sample Data Let us create a collection with nested embedded documents ? db.demo631.insertOne({ id: "101", Info1: [ ...

Read More

Query for values (not objects) in list with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 184 Views

To query for values in list, use the positional operator ($) in MongoDB. The $ operator projects only the first matching array element that satisfies the query condition. Syntax db.collection.find( { "arrayField": "value" }, { "arrayField.$": 1 } ); Sample Data Let us create a collection with documents — db.demo628.insertMany([ { id: 1, Name: ["Chris", "David", "John"] }, { id: 1, Name: ["Carol", "Sam"] }, { id: 2, Name: ["Mike", "Sam", "John"] ...

Read More

Sum with MongoDB group by multiple columns to calculate total marks with duplicate ids

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 850 Views

To sum marks with MongoDB group by multiple columns and calculate total marks with duplicate IDs, use the aggregate() method with $group stage. This allows grouping by multiple fields like ID and Name to sum marks for each unique combination. Syntax db.collection.aggregate([ { $group: { _id: { field1: "$field1", ...

Read More

Remove document whose value is matched with $eq from a MongoDB collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 329 Views

To remove documents from a MongoDB collection using the $eq operator, use the remove() method with a query that matches field values. The $eq operator explicitly matches documents where a field equals the specified value. Syntax db.collection.remove({ fieldName: { $eq: "value" } }); Create Sample Data Let us create a collection with documents − db.demo626.insertMany([ {id: 1, "Name": "Chris"}, {id: 2, "Name": "David"}, {id: 3, "Name": "Bob"}, {id: 4, "Name": "Mike"} ]); ...

Read More

MongoDB - How can I see if all elements of a field are contained in a superset?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

To check if all elements of an array field are contained within a superset in MongoDB, use the $not operator combined with $elemMatch and $nin. This ensures no array element exists outside the specified superset. Syntax db.collection.find({ "arrayField": { $not: { $elemMatch: { $nin: ["superset", "values", "here"] ...

Read More

Can we search an array of objects in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 561 Views

Yes, you can search an array of objects in MongoDB using multiple approaches. The most common methods are direct field matching and the aggregation pipeline with $unwind. Syntax // Direct field matching db.collection.find({"arrayField.fieldName": "value"}) // Using aggregation with $unwind db.collection.aggregate([ {$unwind: "$arrayField"}, {$match: {"arrayField.fieldName": "value"}} ]) Sample Data db.demo623.insertOne({ _id: 1, details: [ { Name: "Chris" ...

Read More
Showing 22971–22980 of 61,297 articles
Advertisements