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 63 of 111
Find in MongoDB documents with filled nested array and reshape the documents result
To find MongoDB documents with filled nested arrays and reshape the results, use the aggregation pipeline with $unwind, $project, and $match operators. This approach filters out empty nested objects and transforms the document structure. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $project: { fieldName: "$arrayField.nestedField" } }, { $match: { fieldName: { $exists: true } } } ]); Create Sample Data db.demo187.insertMany([ { "_id": "101", ...
Read MoreMongoDB query to count the frequency of every element of the array
To count the frequency of every element in an array across all documents in a MongoDB collection, use the aggregation pipeline with $unwind, $group, and $sum operators. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField", count: { $sum: 1 } } } ]); Sample Data db.demo184.insertMany([ { "Names": ["Chris", "David", "Bob"] }, { "Names": ["Chris", "Mike"] }, { "Names": ["Chris", "Bob", "Carol"] } ]); { ...
Read MoreQuery MongoDB subdocument to print on one line?
To display subdocuments in one line format, use $unwind along with aggregate() to flatten array elements, then iterate through results with a custom function. Syntax db.collection.aggregate([ { $match: { "field": "value" } }, { $unwind: "$arrayField" } ]).forEach(function(doc) { print(doc.field1 + ", " + doc.arrayField.subfield + ", " + doc.arrayField.subfield2); }); Sample Data db.demo183.insertOne({ "_id": "110", "DueDate": ISODate("2020-02-04T01:10:42.000Z"), "ProductDetails": [ { ...
Read MoreMongoDB query to search date records using only Month and Day
To search MongoDB documents using only month and day while ignoring the year, use the $where operator with JavaScript functions like getMonth() and getDate(). Syntax db.collection.find({ $where: function() { return this.dateField.getMonth() == monthValue && this.dateField.getDate() == dayValue; } }); Sample Data db.demo181.insertMany([ {"ShippingDate": new ISODate("2020-01-10")}, {"ShippingDate": new ISODate("2019-12-11")}, {"ShippingDate": ...
Read MoreHow to find latest entries in array over all MongoDB documents?
To find latest entries in array over all MongoDB documents, use the aggregate() method with $unwind to flatten arrays, $sort to order by the desired field, and $limit to get the top entries. Syntax db.collection.aggregate([ { "$unwind": "$arrayField" }, { "$sort": { "arrayField.sortField": -1 } }, { "$limit": numberOfEntries } ]); Sample Data db.demo179.insertMany([ { "Name": "Chris", "Details": [ ...
Read MoreMongoDB query to fetch date records (ISODate format) in a range
To fetch date records within a specific range from MongoDB documents containing ISODate format dates, use range operators like $gte, $lte, $gt, and $lt in your queries, or aggregation pipeline operators for more complex date filtering. Syntax // Basic date range query db.collection.find({ "dateField": { "$gte": ISODate("start_date"), "$lte": ISODate("end_date") } }); // Aggregation with time-based filtering db.collection.aggregate([ { "$redact": { ...
Read MoreMongoDB query to add a document in an already created collection
To add a document to an existing collection in MongoDB, use the $push operator. This operator appends new documents to an existing array field within a document. Syntax db.collection.update( { "field": "value" }, { $push: { "arrayField": { newDocument } } } ); Sample Data Let's create a collection with sample documents ? db.demo177.insertOne({ "Id": "101", "details": [ { "StudentName": "Chris", ...
Read MoreGet substring in MongoDB aggregate
To extract substring in MongoDB aggregation pipelines, use the $substr operator within the $project stage. This operator extracts a portion of a string starting from a specified position and length. Syntax db.collection.aggregate([ { $project: { fieldName: { $substr: ["$sourceField", startIndex, length] } } } ]) Sample Data db.demo176.insertMany([ {"ProductName": "PRODUCT-1"}, ...
Read MoreHow do I use MongoDB to count only collections that match two fields?
To count documents in MongoDB that match multiple field conditions, use the count() method with a query document containing all required field−value pairs. MongoDB will return the count of documents where all specified conditions are satisfied. Syntax db.collection.count({ "field1": "value1", "field2": "value2" }); Sample Data db.demo175.insertMany([ {"EmployeeName": "Bob", "isMarried": "YES"}, {"EmployeeName": "David", "isMarried": "NO"}, {"EmployeeName": "Mike", "isMarried": "YES"}, {"EmployeeName": "Sam", "isMarried": "NO"} ]); { ...
Read MoreEvaluate one of more values from a MongoDB collection with documents
To evaluate one or more values from a MongoDB collection, use the $or operator with the find() method. The $or operator performs a logical OR operation on an array of expressions and returns documents that match at least one of the conditions. Syntax db.collection.find({ $or: [ { "field1": "value1" }, { "field2": "value2" }, { "fieldN": "valueN" } ] }); Sample Data ...
Read More