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
MongoDB Articles
Page 7 of 111
MongoDB query to find "where" Billing Address is Equal to Delivery Address from documents?
To find documents where billing address equals delivery address in MongoDB, use the $where operator with JavaScript expressions to compare field values within the same document. Syntax db.collection.find({ $where: "this.field1 == this.field2" }); Sample Data db.demo589.insertMany([ {deliveryAddress: "US", billingAddress: "UK"}, {deliveryAddress: "US", billingAddress: "US"}, {deliveryAddress: "US", billingAddress: "AUS"}, {deliveryAddress: "UK", billingAddress: "US"} ]); { "acknowledged" : true, "insertedIds" : [ ...
Read MoreMongoDB query to gather unique array item?
To gather unique array items from a MongoDB collection, use the distinct() method. This method returns an array of unique values for a specified field, automatically removing duplicates. Syntax db.collection.distinct("fieldName") Sample Data Let us create a collection with documents ? db.demo588.insertOne({ "CountryName": ["US", "AUS", "UK", "US", "UK", "AUS"] }); { "acknowledged": true, "insertedId": ObjectId("5e92bbd2fd2d90c177b5bccb") } Display all documents from the collection ? db.demo588.find().pretty(); { "_id": ObjectId("5e92bbd2fd2d90c177b5bccb"), ...
Read MoreHow to keep appending subdocuments in MongoDB?
To append subdocuments in MongoDB, use the $push operator with the update() method. This operator adds new subdocuments to an existing array field without removing existing elements. Syntax db.collection.update( { "field": "matchValue" }, { "$push": { "arrayField": { subdocumentData } } } ); Sample Data Let us create a collection with documents − db.demo587.insertMany([ { "id": 101, "details": [{ Name: "Chris", Age: 21, Marks: 57 }] }, { "id": 102, "details": [{ Name: "Bob", Age: 22, ...
Read MoreMongoDB: Using reference as key and manually adding a value?
To manually add a value to an array in MongoDB using a reference as a key, use the $push operator. This allows you to add new elements to existing arrays, including complex objects with multiple fields. Syntax db.collection.update( { "field": "matchValue" }, { "$push": { "arrayField": newValue } } ); Sample Data db.demo585.insertMany([ { firstName: "John", lastName: "Doe", ...
Read MoreHow to project grouping into object in MongoDB and display only the marks field?
To project grouping into an object in MongoDB and display only the marks field, you can use JavaScript methods to transform array data into key-value objects. This approach converts subject-marks pairs into a single object where subject names become keys and marks become values. Syntax var resultObject = {}; arrayData.forEach(function(item) { resultObject[item.keyField] = item.valueField; }); Sample Data var document = [ { "SubjectName": "MySQL", "Marks": 78 }, { "SubjectName": "MongoDB", "Marks": 89 }, { "SubjectName": "Java", "Marks": ...
Read MoreMongoDB query to get average in aggregation of array element?
To calculate the average of array elements in MongoDB, use the $avg operator in an aggregation pipeline. This operator computes the arithmetic mean of all numeric values in an array field. Syntax db.collection.aggregate([ { $project: { fieldName: { $avg: "$arrayField" } } } ]) Sample Data Let us create a collection with documents containing an array of marks ? db.demo584.insertOne({ "Marks": [75, 50, 85, 60, 80] }); { "acknowledged": true, "insertedId": ObjectId("5e91d827fd2d90c177b5bcc2") } ...
Read MoreApply the Group Accumulator Operator $first on the system variable ROOT to return reference to the root document?
The $first group accumulator operator combined with the $$ROOT system variable returns a reference to the first complete document encountered in each group. The $$ROOT variable references the entire root document being processed in the aggregation pipeline. Syntax db.collection.aggregate([ { $group: { _id: "$groupingField", firstDocument: { $first: "$$ROOT" } } ...
Read MoreHow do I order a list and add position to its items in MongoDB?
To order a list and add position to its items in MongoDB, use sort() combined with forEach() to iterate through the sorted results and assign position values. This approach permanently adds position data to your documents based on the specified sort order. Syntax var i = 1; db.collection.find().sort({fieldName: 1}).forEach(function(doc) { doc.Position = i; i++; db.collection.save(doc); }); Sample Data db.demo581.insertMany([ {"Name": "Chris", "Score": 56}, {"Name": "Bob", "Score": 240}, {"Name": "David", ...
Read MoreSum unique properties in different collection elements in MongoDB and get the resultant Price?
To calculate the sum of unique properties in different collection elements in MongoDB, use $cond along with $group in an aggregation pipeline. This technique allows you to conditionally select which field to use for grouping and then sum the prices for each unique identifier. Syntax db.collection.aggregate([ { $project: { field1: 1, field2: 1, ...
Read MoreMongoDB query to slice only one element of array
To slice only one element of an array in MongoDB, use the $slice operator in the projection stage of a query. This operator allows you to return a specific number of array elements from the beginning or end of an array. Syntax db.collection.find( {}, { "arrayField": { $slice: N } } ); Where N is the number of elements to return (positive for first N elements, negative for last N elements). Sample Data db.demo579.insertOne({ "_id": 101, ...
Read More