Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Database Articles
Page 91 of 547
Get number of updated documents in MongoDB?
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 MoreFinding highest value from sub-arrays in MongoDB documents?
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 MoreMongoDB order by two fields sum?
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 MoreMongoDB equivalent of WHERE IN(1,2,...)?
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 MoreHow to project specific fields from a document inside an array in Mongodb?
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 MoreHow to query a key having space in its name with MongoDB?
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 MoreHow to query an Array[String] for a Regular Expression match?
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 MoreHow to find and modify a value in a nested array?
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 MoreGet Absolute value with MongoDB aggregation framework?
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 MoreHow to query on top N rows in MongoDB?
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