MongoDB Articles

Page 52 of 111

Implement a MongoDB $cond field in a projection pipeline based on the presence or absence of a field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

To implement a MongoDB $cond field in a projection pipeline based on the presence or absence of a field, use $cond along with $anyElementTrue. NULL values (absence of a field) evaluate to FALSE, and empty arrays also return FALSE with $anyElementTrue. Syntax db.collection.aggregate([ { "$project": { "fieldName": { "$cond": [ ...

Read More

How can I search a collection to find a nested value in one of its documents in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 151 Views

To search for nested values in MongoDB documents, use dot notation in the find() method. This allows you to query fields within embedded objects by specifying the path to the nested field. Syntax db.collection.find({"parentField.nestedField": "value"}); Sample Data Let us first create a collection with nested documents ? db.nestedDemo.insertMany([ {"Information": {"__StudentName": "John Smith"}}, {"Information": {"__StudentName": "John Doe"}}, {"Information": {"__StudentName": "Chris Brown"}} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Find MongoDB document with array containing the maximum occurrence of a specific value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

To find a MongoDB document with an array containing the maximum occurrence of a specific value, use the aggregation pipeline with $filter, $size, and $group operators to count occurrences and identify the document with the highest count. Syntax db.collection.aggregate([ { $project: { "arrayField": 1, "occurrenceCount": { $size: { $filter: ...

Read More

Update only a single document in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

To update only a single document in MongoDB, use the updateOne() method. This method updates the first document that matches the query criteria, even if multiple documents match the filter condition. Syntax db.collection.updateOne( { filter }, { $set: { field: "newValue" } } ); Sample Data Let us first create a collection with documents ? db.updateOneDemo.insertMany([ {"StudentId": 1, "StudentFirstName": "Chris"}, {"StudentId": 2, "StudentFirstName": "David"}, {"StudentId": 1, "StudentFirstName": "Mike"} ]); ...

Read More

Fetch specific field values in MongoDB

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

To fetch specific field values in MongoDB, use the $in operator to match documents where a field value equals any value in a specified array, combined with projection to return only the desired fields. Syntax db.collection.find( { fieldName: { $in: ["value1", "value2", "value3"] } }, { field1: 1, field2: 0, _id: 0 } ); Sample Data Let us create a collection with student documents ? db.indexesDemo.insertMany([ {"StudentFirstName": "John", "StudentLastName": "Smith"}, {"StudentFirstName": "Chris", "StudentLastName": "Brown"}, ...

Read More

Appending an entry in one to many embedded documents with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 381 Views

To append an entry in MongoDB one-to-many embedded documents, use the $push operator which adds new elements to an existing array field without affecting other array elements. Syntax db.collection.update( { "matchField": "value" }, { $push: { "arrayField": newDocument } } ); Create Sample Data Let us create a collection with documents ? db.demo253.insertOne({ _id: "101", isActive: false, details: [ { ...

Read More

Sort array in MongoDB query and project all fields?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 726 Views

To sort an array within a document and project all fields in MongoDB, use the aggregation pipeline with $unwind, $sort, $group, and $project stages. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $sort: { "arrayField.field": 1 } }, { $group: { _id: "$_id", arrayField: { $push: "$arrayField" } } }, { $project: { _id: 1, arrayField: 1 } } ]); Sample Data db.demo252.insertOne({ "Values": [ { ...

Read More

Perform min/max with MongoDB aggregation

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

To find minimum and maximum values in MongoDB, use the $min and $max operators within the aggregation pipeline's $group stage. These operators analyze numeric fields across documents and return the smallest and largest values respectively. Syntax db.collection.aggregate([ { $group: { _id: null, maxValue: { $max: "$fieldName" }, minValue: { ...

Read More

Find the MongoDB collection size for name "Chris

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 148 Views

To find the MongoDB collection size for documents with specific field values, use Object.bsonsize() within a forEach() loop to calculate the total BSON size of matching documents. Syntax var totalSize = 0; db.collection.find({fieldName: "value"}).forEach(function(doc) { totalSize += Object.bsonsize(doc); }); print(totalSize); Sample Data db.demo250.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"}, {"Name": "Chris"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Query BinData by Type in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 832 Views

To query BinData documents by their subtype in MongoDB, use the subtype() method within a JavaScript function to filter documents based on their binary data type. Syntax db.collection.find(function(){ return this.fieldName.subtype() == subtypeNumber }); Sample Data Let us create a collection with documents containing different BinData subtypes − db.demo249.insertMany([ { "_id": BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }, { "_id": BinData(4, "CNDF66qIlCY92q1vFAAAAQ==") }, { "_id": BinData(3, "CNDF66qJ29g92q1vFAAAEw==") } ]); { "acknowledged": true, ...

Read More
Showing 511–520 of 1,106 articles
« Prev 1 50 51 52 53 54 111 Next »
Advertisements