Big Data Analytics Articles

Page 62 of 135

MongoDB aggregation with multiple keys

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 671 Views

MongoDB aggregation with multiple keys allows you to group documents by several fields simultaneously. Use the $group stage with a composite _id object containing multiple grouping fields. Syntax db.collection.aggregate([ { $group: { _id: { field1: "$field1", field2: "$field2" ...

Read More

MongoDB query to sort by the sum of specified object inside inner array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 374 Views

To sort by the sum of specified object inside inner array, use $unwind, $group, and $sort in an aggregation pipeline. This approach counts occurrences of specific values within arrays and sorts documents accordingly. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$_id", total: { $sum: { $cond: { if: { $eq: ["$arrayField.field", value] }, ...

Read More

What kind of MongoDB query finds same value multiple times in an array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 488 Views

To find documents where the same value appears multiple times in an array, use the $where operator with JavaScript functions to filter and count duplicate values within array elements. Syntax db.collection.find({ "$where": function() { return this.arrayField.filter(function(element) { return element.field == targetValue; }).length > 1; } }); Sample Data db.demo188.insertMany([ { ...

Read More

Find in MongoDB documents with filled nested array and reshape the documents result

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 233 Views

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 More

MongoDB query to count the frequency of every element of the array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 891 Views

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 More

Query MongoDB subdocument to print on one line?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 375 Views

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 More

MongoDB query to search date records using only Month and Day

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 845 Views

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 More

How to find latest entries in array over all MongoDB documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 171 Views

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 More

MongoDB query to fetch date records (ISODate format) in a range

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 390 Views

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 More

MongoDB query to add a document in an already created collection

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 244 Views

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 More
Showing 611–620 of 1,348 articles
« Prev 1 60 61 62 63 64 135 Next »
Advertisements