Articles on Trending Technologies

Technical articles with clear explanations and examples

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 479 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 202 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

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 181 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 408 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
Showing 23351–23360 of 61,297 articles
Advertisements