Database Articles

Page 17 of 547

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 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 478 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 262 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 265 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 472 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 139 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 317 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

MongoDB Group query to get the count of repeated marks in documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

To get the count of repeated marks in MongoDB documents, use the $group aggregation stage with $sum to count occurrences of each unique mark value. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } } ]) Sample Data db.demo676.insertMany([ ...

Read More

What does the max field mean in the output of MongoDB db..stats( )?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 136 Views

The max field in MongoDB's db..stats() output represents the maximum number of documents allowed in a capped collection. This field only appears for capped collections and shows the document limit set during collection creation. Syntax db.createCollection("collectionName", { capped: true, size: sizeInBytes, max: maxDocuments }); Create Sample Capped Collection Let's create a capped collection with a maximum of 50 documents ? db.createCollection("demo673", { capped: true, size: 100, ...

Read More

Get fields from multiple sub-documents that match a condition in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 737 Views

To get fields from multiple sub-documents that match a condition in MongoDB, use the aggregation pipeline with $unwind to flatten nested arrays, $match to filter documents, and $project to select specific fields. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.nestedField": "condition" } }, { $project: { fieldName: "$arrayField.targetField" } } ]); Sample Data db.demo671.insertOne({ "details": [ { ...

Read More
Showing 161–170 of 5,468 articles
« Prev 1 15 16 17 18 19 547 Next »
Advertisements