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
Articles on Trending Technologies
Technical articles with clear explanations and examples
MongoDB query to find oldest date of three keys in each document
To find the oldest date among three date fields in each MongoDB document, use the $min operator within an aggregation pipeline. This operator compares multiple date values and returns the smallest (oldest) one. Syntax db.collection.aggregate([ { $project: { OldestDate: { $min: ["$dateField1", "$dateField2", "$dateField3"] } ...
Read MoreMongoDB query to update selected fields
To update selected fields in MongoDB, use the update() method with the $set operator. The $set operator modifies specific fields without affecting other document properties. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "field": "newValue" } } ); Create Sample Data Let us create a collection with sample documents ? db.demo352.insertMany([ { "Name": "Chris" }, { "Name": "David" }, { "Name": "Bob" }, { "Name": "Mike" } ...
Read MoreHow to find specific array elements in MongoDB document with query and filter with range?
To find specific array elements in MongoDB documents with query and filter with range, use the aggregation pipeline with $match to filter documents and $filter to filter array elements based on price range conditions. Syntax db.collection.aggregate([ { $match: { field: "value" } }, { $addFields: { arrayField: { $filter: { ...
Read MoreQuery deeply nested Objects in MongoDB
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 MoreMongoDB findOneAndUpdate() to update a single document
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 MoreQuerying array of Embedded Documents in MongoDB based on Range?
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 MoreMongoDB 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 More