MongoDB Articles

Page 14 of 111

Group with multiple fields and get the count of duplicate field values grouped together innMongoDB

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

To group documents with multiple fields and count duplicate combinations in MongoDB, use the aggregation framework with $project and $group stages. The $cond operator helps normalize field values for consistent grouping regardless of field order. Syntax db.collection.aggregate([ { $project: { field1: { $cond: { if: condition, then: value1, else: value2 } }, field2: { $cond: { if: condition, then: value1, else: ...

Read More

How do I delete array value from a document in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 556 Views

To delete array values from a document in MongoDB, use the $pull operator. The $pull operator removes from an existing array all instances of a value or values that match a specified condition. Syntax db.collection.update( { "matchCondition": "value" }, { $pull: { "arrayField": "valueToRemove" } } ); Create Sample Data Let us first create a collection with documents − db.demo535.insertOne({ "studentId": "101", "studentName": "Chris", "ListOfMailIds": [ ...

Read More

Unable to implement $addToSet in MongoDB to fetch values of a single field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 Views

The $addToSet operator adds values to an array unless the value is already present, in which case $addToSet does nothing to that array. When used with $group in aggregation, it creates a unique set of values from a field across multiple documents. Syntax db.collection.aggregate([ { $group: { _id: groupingField, arrayField: { $addToSet: "$fieldName" } ...

Read More

MongoDB query to fetch only the "Name" field based on roles?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 307 Views

To fetch only the "Name" field based on specific roles in MongoDB, use the aggregation framework with $group, $setIsSubset, and $match operators. This approach filters documents by comparing their roles against a target role set. Syntax db.collection.aggregate([ { $group: { _id: "$Name", "types": { $addToSet: "$Type" } } }, { $project: { _id: 1, "types": 1, "isSubset": { $setIsSubset: [["TargetRole"], "$types"] } } }, { $match: { "isSubset": false } }, { $group: { _id: "$isSubset", "Name": { $push: "$_id" } ...

Read More

How I can use a database-name with special characters like " customer_tracker-990" in MongoDB console

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 475 Views

To use a database name with special characters like "customer_tracker-990" in MongoDB console, use the getSiblingDB() method. This method allows you to reference databases with names that contain special characters like hyphens, spaces, or other symbols. Syntax db.getSiblingDB("database-name-with-special-chars").collection.operation() Sample Data Let us add some documents to the database with special characters − use customer_tracker-990; db.demo1.insertMany([ {"Name": "Chris"}, {"Name": "David"}, {"Name": "Bob"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Get the top most document from a MongoDB collection

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

To get the topmost document from a MongoDB collection, use the find() method along with limit(1). This retrieves the first document based on the natural insertion order or specified sorting criteria. Syntax db.collection.find().limit(1); Sample Data db.demo681.insertMany([ {_id: 101, Name: "Chris"}, {_id: 102, Name: "Bob"}, {_id: 103, Name: "David"}, {_id: 104, Name: "Bob"}, {_id: 105, Name: "Sam"} ]); { "acknowledged": true, "insertedIds": [101, 102, 103, 104, 105] ...

Read More

Set gt condition in MongoDB and

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 263 Views

The $and operator performs a logical AND operation on an array of one or more expressions. You can combine the $gt condition with other criteria using $and to create complex queries that match documents satisfying multiple conditions. Syntax db.collection.find({ $and: [ { field: { $gt: value } }, { field: anotherCondition } ] }); Sample Data db.demo680.insertMany([ { Values: 40 }, ...

Read More

Pull an element in sub of sub-array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

To pull an element from a sub-array within a nested array in MongoDB, use $pull combined with the $ positional operator. This allows you to target and remove specific elements from deeply nested array structures. Syntax db.collection.update( { "parentArray.field": "matchValue" }, { $pull: { "parentArray.$.subArray": { "field": "valueToRemove" } } } ); Create Sample Data db.demo679.insertOne({ id: 1, "details": [ { ...

Read More

How to index my collection to use a compound multikey index?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 135 Views

To create a compound multikey index on your MongoDB collection, use the createIndex() method (modern replacement for the deprecated ensureIndex()). A compound multikey index combines multiple fields where at least one field contains array values. Syntax db.collection.createIndex({ "field1": 1, "arrayField.nestedField": 1 }); Create Sample Data First, let's create the compound multikey index on our collection ? db.demo678.createIndex({id: 1, "details.userId": 1}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, ...

Read More

MongoDB query to add up the values of a specific field in documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 310 Views

To add up the values of a specific field across all documents in MongoDB, use the $sum aggregation operator within a $group stage. This calculates the total sum of numeric field values. Syntax db.collection.aggregate([ { $group: { _id: null, sum: { $sum: "$fieldName" } } } ]); Create Sample Data Let us create a collection with sample documents ? db.demo677.insertMany([ { Value: 10 }, { Value: 50 }, { Value: 20 }, ...

Read More
Showing 131–140 of 1,106 articles
« Prev 1 12 13 14 15 16 111 Next »
Advertisements