Database Articles

Page 12 of 547

MongoDB aggregation to get two documents with the least marks

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 205 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 406 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 391 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 906 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 197 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 249 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

How do I get email-id from a MongoDB document and display with print()

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

To extract email-id values from MongoDB documents and display them using print(), use the forEach() method to iterate through documents and access the email field with dot notation. Syntax db.collection.find().forEach(function(document) { print(document.fieldName); }); Sample Data db.demo690.insertMany([ {"UserName": "John", "UserEmailId": "John@gmail.com"}, {"UserName": "Bob", "UserEmailId": "Bob@gmail.com"}, {"UserName": "David", "UserEmailId": "David@gmail.com"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5ea6db31551299a9f98c939c"), ...

Read More

Increment only a single value in MongoDB document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

To increment only a single value in a MongoDB document, use the $inc operator with the update() method. The $inc operator increases the value of a field by the specified amount. Syntax db.collection.update( { "field": "matchValue" }, { $inc: { "fieldToIncrement": incrementValue } } ); Create Sample Data Let us create a collection with documents containing Score values ? db.demo698.insertMany([ { Score: 78 }, { Score: 56 }, { Score: 65 }, ...

Read More

Build (escape) regexp in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 401 Views

To build and escape regular expressions in MongoDB, use the regex literal syntax with the /pattern/flags format or the $regex operator. The i flag enables case-insensitive matching, allowing you to find documents regardless of text case. Syntax // Method 1: Regex literal db.collection.find({ field: /pattern/flags }); // Method 2: $regex operator db.collection.find({ field: { $regex: "pattern", $options: "flags" } }); Sample Data db.demo696.insertMany([ { Message: "/Good/" }, { Message: "(good)" }, { Message: "/Bye/" }, { ...

Read More
Showing 111–120 of 5,468 articles
« Prev 1 10 11 12 13 14 547 Next »
Advertisements