MongoDB Articles

Page 28 of 111

How to append to array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 385 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

MongoDB aggregation / math operation to sum score of a specific student

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 315 Views

To sum scores of a specific student in MongoDB, use the aggregate() method with $match to filter by student name and $sum to calculate the total score. Syntax db.collection.aggregate([ { "$match": { "Name": "StudentName" } }, { "$group": { "_id": null, "TotalScore": { "$sum": "$Score" } } } ]); Sample Data db.demo434.insertMany([ {"Name": "Chris", "Score": 45}, {"Name": "David", "Score": 55}, {"Name": "Chris", "Score": 55} ]); { ...

Read More

How to filter a query on specific date format with MongoDB?

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

To filter a query on specific date format in MongoDB, use the $dateToString operator within an aggregation pipeline to convert dates to string format and then match against the desired date pattern. Syntax db.collection.aggregate([ { $addFields: { stringDate: { $dateToString: { format: "%Y-%m-%d", date: "$dateField" } } } }, { $match: { "stringDate": "YYYY-MM-DD" } }, { $project: { "stringDate": 0 } } ]); Sample Data db.demo433.insertMany([ { "DueDate": new Date("2019-11-23") }, { ...

Read More

Aggregation framework to get the name of students with test one score less than the total average of all the tests

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

To find students whose test one score is less than the total average of all tests, use MongoDB's aggregation framework with $project, $group, $filter, and $map operators to calculate averages and filter results. Syntax db.collection.aggregate([ { $project: { Name: 1, Value1: 1, average: { $avg: ["$Value1", "$Value2", "$Value3"] } } }, { $group: { _id: null, students: { $push: {Name: "$Name", Value1: "$Value1"} }, totalAverage: { $avg: "$average" } } }, { $project: { filteredNames: { $map: { input: { $filter: { input: "$students", cond: ...

Read More

What is the fastest way to update the whole document (all fields) in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 599 Views

The fastest way to update the whole document (all fields) in MongoDB is to use replaceOne() method. This method replaces the entire document while preserving the _id field. Syntax db.collection.replaceOne( { filter }, { newDocument } ); Sample Data db.demo431.insertMany([ {"Name": "Chris", "Age": 32}, {"Name": "David", "Age": 31}, {"Name": "John", "Age": 24}, {"Name": "Bob", "Age": 22} ]); { "acknowledged": true, ...

Read More

MongoDB query to group records and display a specific value with dot notation

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 267 Views

To group records in MongoDB and display specific values using dot notation, use the $group aggregation stage with field references like "$details.Name" to extract values from nested documents or arrays. Syntax db.collection.aggregate([ { $group: { "_id": { "fieldName": "$arrayField.nestedField" } } } ]); Sample Data db.demo430.insertOne({ "details": [ ...

Read More

MongoDB query to create new field and count set the count of another field in it?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 696 Views

To create a new field and count elements from another field in MongoDB, use $addFields with $map and $size operators. This allows you to transform array elements and count their sizes in a single aggregation. Syntax db.collection.aggregate([ { "$addFields": { "arrayField": { "$map": { ...

Read More

Conditional upsert (multiple insert) when updating document in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 536 Views

To perform conditional upserts and multiple inserts when updating documents in MongoDB, use the bulkWrite() method. This allows you to combine update operations and insert operations in a single transaction-like operation. Syntax db.collection.bulkWrite([ { "updateOne": { "filter": { field: value }, "update": { $set: { newField: newValue } } }}, { "insertOne": { "document": { field1: value1, field2: value2 } ...

Read More

Update a MongoDB document with Student Id and Name

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 607 Views

To update a MongoDB document with Student Id and Name, use the update() method with $set operator. The $set operator modifies specific fields without affecting other document properties. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "field": "newValue" } } ); Sample Data Let us create a collection with student documents : db.demo427.insertMany([ { "StudentId": 101, "StudentName": "Chris Brown" }, { "StudentId": 102, "StudentName": "David Miller" }, { "StudentId": 103, "StudentName": ...

Read More

Match ID and fetch documents with $eq in MongoDB in case of array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

Use the $eq operator along with find() to match ID values within arrays and fetch documents. The $eq specifies an equality condition and matches documents where any array element equals the specified value. Syntax db.collection.find({"arrayField": {$eq: "value"}}); // Alternative (equivalent) syntax db.collection.find({"arrayField": "value"}); Sample Data Let us create a collection with documents ? db.demo426.insertMany([ {"Ids": ["110", "120", "101"]}, {"Ids": ["100", "201", "401"]}, {"Ids": ["501", "600", "700"]} ]); { "acknowledged": true, ...

Read More
Showing 271–280 of 1,106 articles
« Prev 1 26 27 28 29 30 111 Next »
Advertisements