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 39 of 111
MongoDB query to find and return subdocument with criteria?
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 MoreHow to query objects with the longest time period in MongoDB?
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 MoreHow do you convert an array of ObjectIds into an array of embedded documents with a field containing the original array element value?
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 MoreMongoDB query to get only specific fields in nested array documents?
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 MoreMongoDB query to find value in array with multiple criteria (range)
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 MoreMongoDB findById returning a list of documents instead of a single result? How to get only a single document?
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 MoreZip two arrays and create new array of object in a reshaped form with MongoDB
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 MoreWorking with MongoDB concatArrays in project on existing multi-array field
The $concatArrays operator is used to concatenate arrays and return the concatenated array. When working with multi-dimensional arrays, you can combine it with $reduce to flatten and concatenate nested arrays. Syntax { "$project": { "newFieldName": { "$reduce": { "input": "$arrayField", "initialValue": [], "in": { "$concatArrays": ["$$this", "$$value"] } } } ...
Read MoreHow to calculate sum in MongoDB with aggregate()?
To calculate sum in MongoDB, use the $sum operator along with the aggregate() method. The $sum operator is used within a $group stage to calculate the total of numeric field values across documents. Syntax db.collection.aggregate([ { $group: { _id: null, totalFieldName: { $sum: "$fieldName" } } } ]); ...
Read MoreHow to update only one property in MongoDB?
To update only one property in MongoDB, use the $set operator to modify a specific field without affecting other properties of the document. For arrays, use $addToSet or $push operators. Syntax db.collection.updateOne( { "field": "value" }, { $set: { "propertyToUpdate": "newValue" } } ); Sample Data db.demo336.insertMany([ { "Name": "Chris", "Score": [45, 67, 78] }, { "Name": "David", "Score": [89, 93, 47] } ]); { "acknowledged": true, ...
Read More