Database Articles

Page 10 of 547

MongoDB: Using reference as key and manually adding a value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 399 Views

To manually add a value to an array in MongoDB using a reference as a key, use the $push operator. This allows you to add new elements to existing arrays, including complex objects with multiple fields. Syntax db.collection.update( { "field": "matchValue" }, { "$push": { "arrayField": newValue } } ); Sample Data db.demo585.insertMany([ { firstName: "John", lastName: "Doe", ...

Read More

How to project grouping into object in MongoDB and display only the marks field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 152 Views

To project grouping into an object in MongoDB and display only the marks field, you can use JavaScript methods to transform array data into key-value objects. This approach converts subject-marks pairs into a single object where subject names become keys and marks become values. Syntax var resultObject = {}; arrayData.forEach(function(item) { resultObject[item.keyField] = item.valueField; }); Sample Data var document = [ { "SubjectName": "MySQL", "Marks": 78 }, { "SubjectName": "MongoDB", "Marks": 89 }, { "SubjectName": "Java", "Marks": ...

Read More

MongoDB query to get average in aggregation of array element?

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

To calculate the average of array elements in MongoDB, use the $avg operator in an aggregation pipeline. This operator computes the arithmetic mean of all numeric values in an array field. Syntax db.collection.aggregate([ { $project: { fieldName: { $avg: "$arrayField" } } } ]) Sample Data Let us create a collection with documents containing an array of marks ? db.demo584.insertOne({ "Marks": [75, 50, 85, 60, 80] }); { "acknowledged": true, "insertedId": ObjectId("5e91d827fd2d90c177b5bcc2") } ...

Read More

Apply the Group Accumulator Operator $first on the system variable ROOT to return reference to the root document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

The $first group accumulator operator combined with the $$ROOT system variable returns a reference to the first complete document encountered in each group. The $$ROOT variable references the entire root document being processed in the aggregation pipeline. Syntax db.collection.aggregate([ { $group: { _id: "$groupingField", firstDocument: { $first: "$$ROOT" } } ...

Read More

How do I order a list and add position to its items in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 337 Views

To order a list and add position to its items in MongoDB, use sort() combined with forEach() to iterate through the sorted results and assign position values. This approach permanently adds position data to your documents based on the specified sort order. Syntax var i = 1; db.collection.find().sort({fieldName: 1}).forEach(function(doc) { doc.Position = i; i++; db.collection.save(doc); }); Sample Data db.demo581.insertMany([ {"Name": "Chris", "Score": 56}, {"Name": "Bob", "Score": 240}, {"Name": "David", ...

Read More

Sum unique properties in different collection elements in MongoDB and get the resultant Price?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 491 Views

To calculate the sum of unique properties in different collection elements in MongoDB, use $cond along with $group in an aggregation pipeline. This technique allows you to conditionally select which field to use for grouping and then sum the prices for each unique identifier. Syntax db.collection.aggregate([ { $project: { field1: 1, field2: 1, ...

Read More

MongoDB query to slice only one element of array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 Views

To slice only one element of an array in MongoDB, use the $slice operator in the projection stage of a query. This operator allows you to return a specific number of array elements from the beginning or end of an array. Syntax db.collection.find( {}, { "arrayField": { $slice: N } } ); Where N is the number of elements to return (positive for first N elements, negative for last N elements). Sample Data db.demo579.insertOne({ "_id": 101, ...

Read More

Sort MongoDB Collection by Array value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

To sort MongoDB collection by Array value, use aggregate() along with $unwind and $sort. This approach first deconstructs the array elements into separate documents, then sorts by the desired array field. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $sort: { "arrayField.sortField": 1 } } // 1 for ascending, -1 for descending ]); Sample Data db.demo577.insertOne({ "student": { "details": [ { ...

Read More

Sort and get only the first two fields in a "$group" operation in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

To sort documents and get only the first document per group in MongoDB's $group operation, use $sort before grouping, then $first with $$ROOT to capture the entire document, and finally $project to shape the output fields. Syntax db.collection.aggregate([ { $sort: { "field": 1 } }, { $group: { "_id": "$groupField", "result": { $first: "$$ROOT" } }}, { $project: { ...

Read More

Hide id field in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 683 Views

To hide the _id field in MongoDB query results, use the projection parameter in the find() method and set _id: 0. This excludes the _id field from the output while displaying other specified fields. Syntax db.collection.find( { query }, { _id: 0, "field1": 1, "field2": 1 } ); Sample Data db.demo575.insertMany([ {id: 101, Information: {Name: "Chris", Age: 21}}, {id: 102, Information: {Name: "David", Age: 20}}, {id: 101, Information: {Name: "Bob", Age: 23}} ]); ...

Read More
Showing 91–100 of 5,468 articles
« Prev 1 8 9 10 11 12 547 Next »
Advertisements