MongoDB Articles

Page 60 of 111

How can I aggregate nested documents in MongoDB?

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

To aggregate nested documents in MongoDB, you can use the aggregation pipeline with operators like $unwind and $group. The $unwind operator flattens array fields, allowing you to process individual elements within nested structures. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $unwind: "$arrayField.nestedArray" }, { $group: { _id: null, result: { $operation: "$arrayField.nestedArray.field" } } } ]); Sample Data db.aggregateDemo.insertMany([ { "ProductInformation": [ ...

Read More

How to add a column in MongoDB collection?

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

To add a column (field) in MongoDB, you need to update the collection using the $set operator. Since MongoDB is schema-less, adding a new field means updating existing documents to include the new field. Syntax db.collection.updateMany({}, {$set: {"newColumnName": "value"}}); Sample Data Let us create a collection with some sample documents ? db.addColumnDemo.insertMany([ {"StudentId": 101, "StudentName": "Chris"}, {"StudentId": 102, "StudentName": "Robert"}, {"StudentId": 103, "StudentName": "David"} ]); { "acknowledged": true, "insertedIds": ...

Read More

How to get specific data in different formats with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

MongoDB provides different formatting options to retrieve and display data from collections. You can use find() for basic queries and pretty() for formatted output display. Syntax // Basic query db.collection.find(query) // Formatted output db.collection.find(query).pretty() // With limit db.collection.find(query).limit(number) Sample Data db.getSpecificData.insertMany([ { "StudentName": "John", "Information": { "FatherName": "Chris", ...

Read More

MongoDB - How to copy rows into a newly created collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 407 Views

To copy rows from one collection to a newly created collection in MongoDB, use the aggregation pipeline with the $out operator. This operator writes the aggregation results to a specified collection. Syntax db.sourceCollection.aggregate([ { $match: {} }, { $out: "destinationCollection" } ]); Sample Data Let us first create a source collection with employee documents ? db.sourceCollection.insertMany([ {"EmployeeName": "Robert", "EmployeeSalary": 15000}, {"EmployeeName": "David", "EmployeeSalary": 25000}, {"EmployeeName": "Mike", "EmployeeSalary": 29000} ]); ...

Read More

Replace value with a string literal during MongoDB aggregation operation

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 Views

Use MongoDB $literal operator in aggregation pipelines to replace field values with fixed string constants during projection operations. The $literal operator treats its value as a literal value, preventing MongoDB from interpreting it as a field reference or expression. Syntax db.collection.aggregate([ { $project: { fieldName: { $literal: "string_value" } } } ]); Sample Data Let us first create ...

Read More

How to count and sum a field between 2 dates in MongoDB?

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

To count and sum a field between 2 dates in MongoDB, use the aggregation pipeline with $match to filter documents by date range using $gte and $lte, then $group with $sum to calculate totals. Syntax db.collection.aggregate([ { $match: { dateField: { $gte: ISODate("start-date"), ...

Read More

Build index in the background with MongoDB

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

To create index in the background, use createIndex() method and set "background: true" as the second parameter. Background indexing allows MongoDB to build indexes without blocking database operations. Syntax db.collectionName.createIndex( {"fieldName1": 1, "fieldName2": 1}, {background: true} ); Example Let us create a compound index on StudentName and StudentAge fields in the background ? db.indexCreationDemo.createIndex( {"StudentName": 1, "StudentAge": 1}, {background: true} ); { "createdCollectionAutomatically": true, "numIndexesBefore": ...

Read More

Fix: MongoDB Robomongo: db.data.find(...).collation is not a function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 153 Views

The collation feature was introduced in MongoDB version 3.4. If you're encountering the error "collation is not a function", you're likely using an older MongoDB version or an outdated client like Robomongo that doesn't support this feature. Check MongoDB Version First, verify your MongoDB version to ensure it's 3.4 or higher ? db.version() 4.0.5 Create Sample Data Let's create a collection with an index that uses collation and insert test documents ? db.collationExample.createIndex( {Value: 1}, {collation: {locale: "en", strength: ...

Read More

Calculating average value per document with sort in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

To calculate the average value per document in MongoDB, use the aggregation framework with $addFields and $avg operators. You can combine this with $sort to order results by the calculated average. Syntax db.collection.aggregate([ { $addFields: { "avgField": { $avg: "$arrayField" } } }, { $sort: { "avgField": 1 } } ]); Sample Data db.calculateAverage.insertMany([ { "Value": [10, 20, 80] }, { "Value": [12, 15, 16] }, { "Value": [30, 35, 40] } ]); ...

Read More

Promote subfields to top level in projection without listing all keys in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

To promote subfields to top level in projection without listing all keys in MongoDB, use $replaceRoot with $arrayToObject and $objectToArray operators. This technique flattens nested objects by converting them to arrays and reconstructing them at the root level. Syntax db.collection.aggregate([ { "$replaceRoot": { "newRoot": { "$arrayToObject": { ...

Read More
Showing 591–600 of 1,106 articles
« Prev 1 58 59 60 61 62 111 Next »
Advertisements