Big Data Analytics Articles

Page 38 of 135

Query deeply nested Objects in MongoDB

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

To query deeply nested objects in MongoDB, use dot notation to navigate through multiple levels of embedded documents and arrays. The dot notation allows you to access fields at any depth within the document structure. Syntax db.collection.find({"level1.level2.level3.field": "value"}); Sample Data Let us create a collection with deeply nested documents ? db.demo350.insertMany([ { id: 101, Name: "Chris", details: [ ...

Read More

MongoDB findOneAndUpdate() to update a single document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 367 Views

The findOneAndUpdate() method is used to update only a single document in MongoDB. It finds the first document that matches the query criteria and updates it, returning either the original or updated document. Syntax db.collection.findOneAndUpdate( filter, update, options ) Sample Data Let us create a collection with student documents − db.demo349.insertMany([ {"Name": "Chris", "Marks": 56}, {"Name": "David", "Marks": 78}, {"Name": "Chris", "Marks": 89}, {"Name": ...

Read More

Querying array of Embedded Documents in MongoDB based on Range?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 263 Views

To query an array of embedded documents based on range, use the $filter operator within an aggregation pipeline. This allows filtering array elements that match specific criteria while preserving the document structure. Syntax db.collection.aggregate([ { $addFields: { "arrayField": { $filter: { ...

Read More

MongoDB query to find and return subdocument with criteria?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 609 Views

To find and return subdocuments that match specific criteria in MongoDB, use the aggregation pipeline with $match to filter documents and $filter to return only matching subdocuments from arrays. Syntax db.collection.aggregate([ { $match: { "fieldName": "value" } }, { $addFields: { "arrayField": { $filter: { ...

Read More

How to query objects with the longest time period in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

To query objects with the longest time period in MongoDB, use the aggregation pipeline with $addFields to calculate the duration, $sort to order by longest period, and $limit to get the top result. Syntax db.collection.aggregate([ { $addFields: { duration: { $subtract: [ { $toDate: "$endDate" }, { $toDate: "$startDate" } ] } } }, { $sort: { duration: -1 } }, { $limit: 1 } ]); Create Sample Data ...

Read More

How do you convert an array of ObjectIds into an array of embedded documents with a field containing the original array element value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 149 Views

To convert an array of ObjectIds into an array of embedded documents with a field containing the original array element value, use the $map operator within an aggregation pipeline. This transformation embeds each original array element as a value within a new document structure. Syntax db.collection.aggregate([ { $addFields: { arrayField: { $map: { ...

Read More

MongoDB query to get only specific fields in nested array documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 879 Views

To get only specific fields in nested array documents in MongoDB, use $filter with $project in an aggregation pipeline to match conditions and extract required data from deeply nested arrays. Syntax db.collection.aggregate([ { $project: { "field": { $filter: { ...

Read More

MongoDB query to find value in array with multiple criteria (range)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 520 Views

To find values in an array within a specific range in MongoDB, use $elemMatch with $gt and $lt operators. This allows you to match documents where at least one array element satisfies multiple criteria. Syntax db.collection.find({ "arrayField": { "$elemMatch": { "field": { "$gt": minValue, ...

Read More

MongoDB findById returning a list of documents instead of a single result? How to get only a single document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

To get only a single result instead of a list when querying by ID in MongoDB, use the findOne() method instead of find(). The findOne() method returns exactly one document that matches your query criteria. Syntax db.collection.findOne({_id: ObjectId("id_value")}) Sample Data Let us create a collection with documents − db.demo340.insertMany([ {_id: 1, "Name": "Chris", Age: 21}, {_id: 2, "Name": "David", Age: 23}, {_id: 3, "Name": "Bob", Age: 20}, {_id: 4, "Name": "Sam", Age: 19} ]); ...

Read More

Zip two arrays and create new array of object in a reshaped form with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 306 Views

To zip two arrays and create a new array of objects in MongoDB, use the $zip operator within an aggregation pipeline. This combines elements from multiple arrays at corresponding positions into a single reshaped structure. Syntax db.collection.aggregate([ { $project: { "newFieldName": { $map: { ...

Read More
Showing 371–380 of 1,348 articles
« Prev 1 36 37 38 39 40 135 Next »
Advertisements