Big Data Analytics Articles

Page 6 of 135

MongoDB query to update nested document

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

To update a nested document in MongoDB, use the $ positional operator with the $set operator. The $ operator identifies the array element that matches the query condition and allows you to update specific fields within that element. Syntax db.collection.update( {"arrayName.field": "matchValue"}, { $set: { "arrayName.$.field": "newValue" } } ); Create Sample Data Let us create a collection with documents − db.demo595.insertOne({ "Information": [ { "_id": new ObjectId(), "Name": "Chris" }, ...

Read More

MongoDB query to limit the returning values of a field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

To limit the returning values of a field in MongoDB, use the $slice operator in the projection parameter of the find() method. This is particularly useful for limiting array elements returned in query results. Syntax db.collection.find( {}, { "arrayField": { "$slice": numberOfElements } } ); Sample Data db.demo594.insertOne({ id: 1, details: [ { Name: "Chris", Age: 21 }, { Name: "Bob", ...

Read More

Fetch specific multiple documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 546 Views

To fetch specific multiple documents in MongoDB, use the $in operator to match documents where a field value equals any value in a specified array. Syntax db.collection.find({ "field": { $in: [value1, value2, value3, ...] } }); Sample Data Let us create a collection with documents − db.demo593.insertMany([ { id: 1, "Name": "Chris" }, { id: 2, "Name": "John" }, { id: 3, "Name": "Bob" }, { id: 4, "Name": "Sam" } ]); ...

Read More

MongoDB query to match documents that contain an array field

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 305 Views

To match documents that contain an array field, use the $elemMatch operator. This operator matches documents where at least one array element meets all specified criteria within a single element. Syntax db.collection.find({ "arrayField": { "$elemMatch": { "field1": "value1", "field2": "value2" } } }); Sample Data Let ...

Read More

MongoDB query to find "where" Billing Address is Equal to Delivery Address from documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

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 More

MongoDB query to gather unique array item?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 Views

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 More

How to keep appending subdocuments in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

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 More

MongoDB: Using reference as key and manually adding a value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 399 Views

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 More

How to project grouping into object in MongoDB and display only the marks field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 152 Views

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 More

MongoDB query to get average in aggregation of array element?

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

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 More
Showing 51–60 of 1,348 articles
« Prev 1 4 5 6 7 8 135 Next »
Advertisements