Database Articles

Page 91 of 547

Get number of updated documents in MongoDB?

George John
George John
Updated on 15-Mar-2026 334 Views

To get the number of updated documents in MongoDB, you can use the WriteResult returned by update operations or the modern modifiedCount property in newer MongoDB versions. Syntax // Legacy update() method db.collection.update(query, update, options); WriteResult({ "nMatched": X, "nModified": Y }) // Modern updateMany() method db.collection.updateMany(query, update); { "modifiedCount": Y } Sample Data db.getNumberOfUpdatedDocumentsDemo.insertMany([ {"StudentName": "David"}, {"StudentName": "Chris"}, {"StudentName": "Robert"}, {"StudentName": "Ramit"}, {"StudentName": "Adam"} ]); { ...

Read More

Finding highest value from sub-arrays in MongoDB documents?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 302 Views

To find the highest value from sub-arrays in MongoDB documents, use the aggregation framework with $unwind, $sort, and $limit operators to flatten arrays, sort by the target field, and return the top result. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $sort: { "arrayField.valueField": -1 } }, { $limit: 1 } ]); Sample Data db.findHighestValueDemo.insertMany([ { _id: 10001, "StudentDetails": [ ...

Read More

MongoDB order by two fields sum?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 535 Views

To order documents by the sum of two fields in MongoDB, use the aggregation framework with $project to calculate the sum and $sort to order the results. Syntax db.collection.aggregate([ { $project: { field1: 1, field2: 1, sumField: { $add: ["$field1", "$field2"] } } }, { $sort: { sumField: 1 } } // 1 for ascending, -1 for descending ]); Sample Data db.orderByTwoFieldsDemo.insertMany([ { "Value1": 10, "Value2": 35 }, { "Value1": 12, "Value2": 5 }, ...

Read More

MongoDB equivalent of WHERE IN(1,2,...)?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 288 Views

The MongoDB equivalent of SQL's WHERE IN(1, 2, ...) clause is the $in operator. This operator allows you to match documents where a field value equals any value in a specified array. Syntax db.collectionName.find({ fieldName: { $in: [value1, value2, value3, ...] } }); Sample Data Let us create a collection with student documents ? db.whereInDemo.insertMany([ { "StudentName": "John", "StudentMathScore": 57 }, { "StudentName": "Larry", "StudentMathScore": 89 }, { "StudentName": "Chris", "StudentMathScore": 98 }, ...

Read More

How to project specific fields from a document inside an array in Mongodb?

George John
George John
Updated on 15-Mar-2026 391 Views

To project specific fields from a document inside an array in MongoDB, use the positional operator ($) in the projection parameter. This returns only the first matching array element that satisfies the query condition. Syntax db.collection.find( { "arrayField.subField": "matchValue" }, { "arrayField.$": 1 } ); Sample Data Let us create a collection with sample documents ? db.projectSpecificFieldDemo.insertMany([ { "UniqueId": 101, "StudentDetails": [ ...

Read More

How to query a key having space in its name with MongoDB?

George John
George John
Updated on 15-Mar-2026 2K+ Views

To query a key having space in its name in MongoDB, wrap the field name with spaces in quotes and use dot notation for nested fields. Syntax db.collection.find({"field name with spaces": "value"}); db.collection.find({"parent.field name with spaces": "value"}); Create Sample Data First, let's create a document with a field that has spaces in its name ? var myValues = {}; myValues["Details"] = {}; myValues["Details"]["Student Name"] = "John"; myValues["Details"]["StudentAge"] = 26; db.keyHavingSpaceDemo.insertOne(myValues); { "acknowledged": true, "insertedId": ObjectId("5ca27e3b6304881c5ce84ba4") } Verify Sample ...

Read More

How to query an Array[String] for a Regular Expression match?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 246 Views

To query an array of strings for a regular expression match in MongoDB, use the regular expression pattern directly in the find query. MongoDB automatically searches through array elements and matches any string that satisfies the regex pattern. Syntax db.collection.find({ arrayField: /regexPattern/ }) Create Sample Data db.queryArrayDemo.insertMany([ { "StudentFullName": [ "Carol Taylor", "Caroline Williams", ...

Read More

How to find and modify a value in a nested array?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 269 Views

To find and modify a value in a nested array, use the $elemMatch operator to locate the array element and the $ positional operator with $set to update the specific field value. Syntax db.collection.update( { arrayName: { $elemMatch: { field: "matchValue" } } }, { $set: { "arrayName.$.targetField": "newValue" } } ); Sample Data db.findAndModifyAValueInNestedArrayDemo.insertOne({ "CompanyName": "Amazon", "DeveloperDetails": [ { ...

Read More

Get Absolute value with MongoDB aggregation framework?

George John
George John
Updated on 15-Mar-2026 313 Views

To get absolute values in MongoDB aggregation framework, use the $abs operator within the $project stage. The $abs operator returns the absolute value of a number, converting negative numbers to positive while keeping positive numbers unchanged. Syntax db.collection.aggregate([ { $project: { fieldName: { $abs: "$numberField" } } } ]); Sample Data Let us create a collection with positive, ...

Read More

How to query on top N rows in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 3K+ Views

To query the top N rows in MongoDB, use the aggregation framework with $sort and $limit operators. This approach allows you to sort documents by any field and retrieve only the specified number of top results. Syntax db.collection.aggregate([ { $sort: { fieldName: 1 } }, // 1 for ascending, -1 for descending { $limit: N } // N = number of documents to return ]); Sample Data db.topNRowsDemo.insertMany([ ...

Read More
Showing 901–910 of 5,468 articles
« Prev 1 89 90 91 92 93 547 Next »
Advertisements