MongoDB Articles

Page 59 of 111

How to calculate sum of string in MongoDB?

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

To calculate sum of string values in MongoDB, use the aggregate pipeline with $group and $sum operators. Convert string numbers to integers using $toInt before summing them. Syntax db.collection.aggregate([ { $group: { _id: null, totalSum: { $sum: { $toInt: "$fieldName" } ...

Read More

Is there a MongoDB query to concatenate deep sub-lists?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

To concatenate deep sub-lists in MongoDB, use the $unwind operator in an aggregation pipeline to flatten nested arrays, then apply additional transformations like sorting, limiting, and reshaping the results. Syntax db.collection.aggregate([ { $unwind: "$arrayField1" }, { $unwind: "$arrayField1.nestedArray" }, { $sort: { "arrayField1.nestedArray.field": 1 } }, { $replaceRoot: { newRoot: "$arrayField1.nestedArray" } } ]); Sample Data db.demo70.insertOne({ "first": [ { ...

Read More

MongoDB, finding all documents where property id equals to record id?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 318 Views

To find all documents where a property id equals the record's _id in MongoDB, use the $where operator with a JavaScript function that compares the field values after converting them to strings. Syntax db.collection.find({ $where: function(){ return this._id.toString() == this.fieldName.toString(); } }); Sample Data db.demo69.insertMany([ { "_id": ObjectId("507c7f79bcf86cd7994f6c0e"), "Details": { ...

Read More

MongoDB aggregate query to sort

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

The $sort stage in MongoDB aggregation pipeline sorts documents by one or more fields. Use 1 for ascending order and -1 for descending order. You can combine it with other stages like $match to filter and sort results. Syntax db.collection.aggregate([ { $match: { field: value } }, { $sort: { field: 1 } } // 1 for ascending, -1 for descending ]); Sample Data db.demo67.insertMany([ { "StudentAge": 23 }, { "StudentAge": 21 }, ...

Read More

How to get the index of an array element in older versions on MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 Views

To get the index of an array element in older versions of MongoDB, use the $indexOfArray aggregation operator. This operator returns the position (0-based index) of the first occurrence of a specified value in an array, or -1 if the value is not found. Syntax db.collection.aggregate([ { $project: { fieldName: { $indexOfArray: ["$arrayField", "searchValue"] } } } ]); Sample ...

Read More

MongoDB aggregation to sum product price with similar IDs

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 478 Views

To sum product prices with similar IDs in MongoDB, use the $group stage in aggregation pipeline with $sum operator. This groups documents by a specified field and calculates the total for each group. Syntax db.collection.aggregate([ { $group: { _id: "$groupingField", totalFieldName: { $sum: "$fieldToSum" } } } ]); ...

Read More

How to properly use 'exist' function in MongoDB like in SQL?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 201 Views

To check for existence of a record in MongoDB (similar to SQL's EXISTS function), use findOne() method. This returns the first matching document if it exists, or null if no document matches the query criteria. Syntax db.collection.findOne({ field: "value" }) Sample Data db.existsAlternateDemo.insertMany([ { "StudentName": "Chris" }, { "StudentName": "Chris", "StudentAge": 21 }, { "StudentName": "Chris", "StudentAge": 22, "StudentCountryName": "US" } ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Insert in MongoDB without duplicates

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

To insert records in MongoDB and avoid duplicates, create a unique index on the field that should not contain duplicate values. When you attempt to insert a document with a duplicate value, MongoDB will reject the operation with an error. Syntax db.collection.createIndex({"fieldName": 1}, { unique: true }); db.collection.insert({"fieldName": "value"}); Example Let's create a unique index on the StudentFirstName field and attempt to insert duplicate records ? Step 1: Create Unique Index db.insertWithoutDuplicateDemo.createIndex({"StudentFirstName":1}, { unique: true }); { "createdCollectionAutomatically" : true, "numIndexesBefore" : ...

Read More

Apply a condition inside subset in MongoDB Aggregation?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 388 Views

To apply a condition inside subset in MongoDB aggregation, use $setIsSubset. This operator checks whether a specified set is a subset of another set, returning true if all elements exist in the target array. Syntax { $setIsSubset: [, ] } Sample Data db.subsetDemo.insertMany([ { "StudentName": "Chris", "StudentFavouriteSubject": ["Java", "Python"] }, { "StudentName": "Chris", ...

Read More

Getting MongoDB results from the previous month

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

To get MongoDB results from the previous month, first get the current date and subtract one month from it. This creates a date boundary to filter records that fall within the previous month period. Syntax var previousMonth = new Date(); previousMonth.setMonth(previousMonth.getMonth() - 1); db.collection.find({dateField: {$gte: previousMonth}}); Sample Data db.findOneMonthAgoData.insertMany([ {"CustomerName": "Chris", "PurchaseDate": new ISODate("2019-12-26")}, {"CustomerName": "David", "PurchaseDate": new ISODate("2019-11-26")}, {"CustomerName": "Bob", "PurchaseDate": new ISODate("2020-11-26")} ]); { "acknowledged": true, "insertedIds": [ ...

Read More
Showing 581–590 of 1,106 articles
« Prev 1 57 58 59 60 61 111 Next »
Advertisements