MongoDB Articles

Page 44 of 111

MongoDB query to update an array using FindAndUpdate()?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 417 Views

To update an array in MongoDB, use the findAndModify() method combined with the $ positional operator to target specific array elements. This method updates the document and returns either the original or updated version. Syntax db.collection.findAndModify({ query: { "arrayField": "matchValue" }, update: { $set: { "arrayField.$": "newValue" } } }); Sample Data db.demo152.insertMany([ {"id": 102, "Name": ["Chris", "David"], "Score": 45}, {"id": 103, "Name": ["Mike", "Carol"], "Score": 65} ]); { "acknowledged": ...

Read More

MongoDB projection result as an array of selected items?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

In MongoDB, projection results as an array of selected items can be achieved using the distinct() method. This method returns an array of distinct values for a specified field from documents that match the given query criteria. Syntax db.collection.distinct(field, query) Where field is the field to return distinct values from, and query is the optional filter criteria. Sample Data Let us create a collection with documents: db.demo151.insertMany([ {"ListOfNames": ["Chris", "David", "Mike"]}, {"ListOfNames": ["Mike", "Bob"]}, {"ListOfNames": ["John", "David", "Chris"]} ]); ...

Read More

Conditional update depending on field matched in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 501 Views

To perform a conditional update in MongoDB, use the update() method with a query condition that matches specific field values, then apply $set to modify the target field. Only documents matching the condition will be updated. Syntax db.collection.update( { "fieldName": "matchValue" }, { $set: { "targetField": "newValue" } } ); Sample Data db.demo150.insertMany([ { "StudentId": 101, "StudentName": "Chris", "StudentMarks": 35 }, { "StudentId": 102, "StudentName": "Chris", "StudentMarks": 55 }, { "StudentId": 103, "StudentName": ...

Read More

MongoDB $elemMatch to match document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 388 Views

The $elemMatch operator in MongoDB matches documents that contain an array field with at least one element matching all specified query criteria. It's particularly useful when working with arrays of embedded documents. Syntax db.collection.find( { arrayField: { $elemMatch: { field1: value1, field2: value2 } } } ); Sample Data Let us create a collection with documents − db.demo313.insertMany([ { "_id": 100, "details": [{ "Name": "Chris", "Age": 24 }] }, { "_id": 101, "details": [{ "Name": "David", "Age": 22 }] }, ...

Read More

MongoDB - update partial number of documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 256 Views

To update multiple documents in MongoDB, use the multi: true option in the update() method. By default, MongoDB updates only the first matching document. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "field": "newValue" } }, { multi: true } ); Sample Data db.demo312.insertMany([ { "FirstName": "Robert" }, { "FirstName": "Bob" }, { "FirstName": "Robert" }, { "FirstName": "David" }, ...

Read More

Get the count of a specific value in MongoDB quickly

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

To get the count of a specific value in MongoDB quickly, use the countDocuments() method with a query filter. For optimal performance, create an index on the field you're counting. Syntax db.collection.countDocuments({"field": "value"}); Create Index for Performance First, create an index on the Name field for faster counting ? db.demo311.createIndex({"Name": 1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } Sample Data db.demo311.insertMany([ ...

Read More

Retrieving a Subset of Fields from MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 713 Views

To retrieve a subset of fields from MongoDB documents, use field projection in the find() method. You can specify which fields to include (1) or exclude (0) from the results using dot notation for nested fields. Syntax db.collection.find( { query }, { "field1": 1, "field2": 0, "nested.field": 1 } ); Sample Data db.demo307.insertMany([ { "ClientId": 101, "ClientDetails": { "ClientFirstName": "Chris", "Age": 34 }, ...

Read More

Fetching all documents from MongoDB Collection in a beautified form

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 392 Views

To fetch all documents from a MongoDB collection in a beautified, readable format, use the find() method combined with the pretty() method. The pretty() method formats the JSON output with proper indentation and line breaks. Syntax db.collectionName.find().pretty(); Create Sample Data Let us create a collection with some sample documents ? db.demo306.insertMany([ {"Name": "Robert", "Age": 21}, {"Name": "David", "Age": 23}, {"Name": "Mike", "Age": 25}, {"Name": "Carol", "Age": 26} ]); { ...

Read More

How to get the child of a MongoDB collection by the key?

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

To get the child of a collection in MongoDB by a specific key, use the find() method with projection. This allows you to retrieve only specific nested fields from documents that match your query criteria. Syntax db.collection.find( { "field": "value" }, { "nestedObject.key": 1 } ); Sample Data db.demo305.insertMany([ { _id: 101, FirstName: "Chris", details: { ...

Read More

Find values with OR operator in MongoDB and format the result.?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

Use $or operator to fetch values matching any of multiple conditions and pretty() to format the result with proper indentation and readability. Syntax db.collection.find({ $or: [ { field1: value1 }, { field2: value2 } ] }).pretty(); Sample Data Let us create a collection with documents: db.demo304.insertMany([ { "StudentName": "Chris", "StudentAge": 23 }, { "StudentName": "David", "StudentAge": 22 }, ...

Read More
Showing 431–440 of 1,106 articles
« Prev 1 42 43 44 45 46 111 Next »
Advertisements