MongoDB Articles

Page 9 of 111

MongoDB compound conditions to fetch documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

MongoDB compound conditions allow you to fetch documents by combining multiple criteria using logical operators. This enables complex filtering with AND, OR, and other conditional operators. Syntax // AND condition (implicit) db.collection.find({field1: "value1", field2: "value2"}) // OR condition db.collection.find({$or: [{field1: "value1"}, {field2: "value2"}]}) // IN operator for multiple values db.collection.find({field: {$in: ["value1", "value2"]}}) Sample Data db.demo714.insertMany([ {FirstName: "Chris", LastName: "Brown"}, {FirstName: "Adam", LastName: "Smith"}, {FirstName: "David", LastName: "Miller"}, {FirstName: "John", LastName: "Doe"} ]); ...

Read More

Update tag records in MongoDB quickly

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

To update tag records in MongoDB quickly, use the $ positional operator along with the update command. The $ operator identifies the first array element that matches the query condition and updates it efficiently. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $set: { "arrayField.$.field": "newValue" } } ); Create Sample Data Let us create a collection with tag documents ? db.demo713.insertOne({ tags: [ { ...

Read More

Sort id and reverse the items with MongoDB

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

The $natural operator returns documents in natural insertion order. To reverse the items and get them in reverse insertion order, use $natural:-1 with the sort() method. Syntax db.collection.find().sort({$natural: -1}); Sample Data Let us create a collection with documents ? db.demo710.insertMany([ {id: 101, Name: "Robert"}, {id: 102, Name: "Carol"}, {id: 103, Name: "Mike"}, {id: 104, Name: "Sam"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

MongoDB aggregation to get two documents with the least marks

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

To get the two documents with the least marks in MongoDB, use the aggregation pipeline with $sort to order by marks in ascending order and $limit: 2 to restrict the result to only two documents. Syntax db.collection.aggregate([ { $sort: { "field": 1 } }, { $limit: 2 } ]); Sample Data db.demo709.insertMany([ { "Name": "John", "Marks": 75 }, { "Name": "Chris", "Marks": 45 }, { "Name": "David", "Marks": 54 }, ...

Read More

Find MongoDB records based on a condition?

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

To find MongoDB records based on a condition, use the find() method with a query document that specifies the filtering criteria. The query document contains field-value pairs that define the conditions to match. Syntax db.collection.find({ field: value }); db.collection.find({ field: { $operator: value } }); Sample Data Let us create a collection with student documents ? db.students.insertMany([ { "Name": "John", "Marks": 54 }, { "Name": "Chris", "Marks": 35 }, { "Name": "David", "Marks": 45 }, { ...

Read More

Set server status to inactive in a MongoDB collection with server records?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 403 Views

To set a server status to inactive in a MongoDB collection, use the positional operator $ with the $set update operator to target specific array elements based on matching criteria. Syntax db.collection.update( { "arrayField.matchField": "matchValue" }, { $set: { "arrayField.$.updateField": "newValue" } } ); Sample Data Let us create a collection with server records − db.demo707.insertOne({ id: 101, "serverInformation": [ { ...

Read More

Display MongoDB with document and subdocument example and update

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 382 Views

MongoDB allows storing documents with subdocuments using nested arrays and objects. This structure enables complex data relationships within a single document, making it ideal for hierarchical data. Syntax db.collectionName.insertOne({ fieldName: "value", subdocumentArray: [ { nestedField1: "value1", nestedField2: "value2" } ] }); Sample Data ...

Read More

MongoDB query to match documents with array values greater than a specific value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 902 Views

To match documents with array values greater than a specific value in MongoDB, use the $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Syntax db.collection.find( { "arrayField": { $elemMatch: { $gt: value } } } ); Create Sample Data Let us create a collection with documents − db.demo701.insertMany([ { "ListOfValues": [100, 200, 300] }, { "ListOfValues": [500, 700, 1000] }, ...

Read More

MongoDB query to display documents with a specific name irrespective of case

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

To display documents with a specific name irrespective of case in MongoDB, use the $regex operator with the i flag for case-insensitive matching. This allows you to find documents containing a specific string value regardless of uppercase or lowercase letters. Syntax db.collection.find({ "field": { $regex: /pattern/i } }); Sample Data db.demo700.insertMany([ { details: [{ Name: "david" }] }, { details: [{ Name: "Chris" }] }, { details: [{ Name: "DAVID" }] }, { ...

Read More

Multiple atomic updates using MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 246 Views

Multiple atomic updates in MongoDB allow you to update multiple documents that match a query condition in a single operation. Use the updateMany() method or the legacy update() method with the multi parameter set to true. Syntax // Modern approach db.collection.updateMany( { filter }, { $set: { field: "newValue" } } ); // Legacy approach db.collection.update( { filter }, { $set: { field: "newValue" } }, { multi: true } ); Sample Data ...

Read More
Showing 81–90 of 1,106 articles
« Prev 1 7 8 9 10 11 111 Next »
Advertisements