Big Data Analytics Articles

Page 17 of 135

Get distinct pair of objects with all subdocuments in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 412 Views

To get a distinct pair of objects with all subdocuments in MongoDB, use the $group aggregation operator to group documents by a specific field and aggregate their values. This technique helps combine duplicate entries into unique pairs with consolidated data. Syntax db.collection.aggregate([ { $group: { _id: "$fieldToGroupBy", aggregatedField: { $operator: "$fieldToAggregate" } } ...

Read More

How to get latest set of data from a MongoDB collection based on the date records?

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

To get the latest set of data from MongoDB collection based on date records, use sort() with -1 for descending order. For a single latest document, combine with limit(1). Syntax // Get all records sorted by date (latest first) db.collection.find().sort({"dateField": -1}); // Get only the latest record db.collection.find().sort({"dateField": -1}).limit(1); Sample Data db.demo521.insertMany([ {"PurchaseDate": new ISODate("2019-01-10"), "ProductName": "Product-1"}, {"PurchaseDate": new ISODate("2020-04-05"), "ProductName": "Product-10"}, {"PurchaseDate": new ISODate("2010-05-08"), "ProductName": "Product-4"}, {"PurchaseDate": new ISODate("2020-02-21"), "ProductName": "Product-3"} ]); ...

Read More

Text search in MongoDB with Regular Expression

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

For text search in MongoDB with Regular Expression, use $regex operator to match patterns within string fields. This allows you to search for documents containing specific text patterns or substrings. Syntax db.collection.find({ "fieldName": { $regex: /pattern/ } }); // Alternative syntax db.collection.find({ "fieldName": { $regex: "pattern", $options: "i" } }); Sample Data db.demo519.insertMany([ {"Value": "50, 60, 70"}, {"Value": "80, 90, 50"}, {"Value": "10, 30, 40"} ]); { ...

Read More

How can I change the field name in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

To change the field name in MongoDB, use the $project operator in an aggregation pipeline. This creates a projection with the new field name while mapping the value from the original field. Syntax db.collection.aggregate([ { $project: { "newFieldName": "$originalFieldName" } } ]); Create Sample Data Let us create a collection with documents − db.demo517.insertMany([ { "Name": "Chris Brown" }, { "Name": "David Miller" }, { "Name": "John Doe" } ]); { ...

Read More

Update the last row with search criteria in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 432 Views

To update the last row that matches specific search criteria in MongoDB, use the findAndModify() method with a sort parameter to identify the most recent document. Syntax db.collection.findAndModify({ query: { field: "value" }, sort: { _id: -1 }, update: { $set: { field: "newValue" } }, new: true }); Sample Data db.demo516.insertMany([ { "Name": "John", "Age": 22, "Score": 56 }, { "Name": "John", "Age": 23, "Score": 67 }, ...

Read More

MongoDB: Find name similar to Regular Expression input?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

To find names similar to a regular expression input in MongoDB, use the $regex operator with the $options parameter for case-insensitive matching. Syntax db.collection.find({ "fieldName": { $regex: "pattern", $options: "i" } }); Sample Data db.demo514.insertMany([ {"Information": {"FullName": "John Doe"}}, {"Information": {"FullName": "John Smith"}}, {"Information": {"FullName": "john doe"}}, {"Information": {"FullName": "Chris Brown"}} ]); ...

Read More

Sum the score of duplicate column values in MongoDB documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 243 Views

To sum values of duplicate column entries in MongoDB, use the $group stage with $sum aggregation operator. This groups documents by a field and calculates the total of specified numeric fields. Syntax db.collection.aggregate([ { $group: { _id: "$groupingField", field1Sum: { $sum: "$field1" }, field2Sum: { $sum: "$field2" } ...

Read More

MongoDB query to find matching documents given an array with values?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

To find matching documents given an array with values in MongoDB, use the $in operator. This operator matches documents where the array field contains any of the specified values. Syntax db.collection.find({ "arrayField": { $in: ["value1", "value2", "value3"] } }); Create Sample Data Let us create a collection with documents containing project arrays ? db.demo511.insertMany([ { "ListOfProject": ["Library Management System", "Hospital Management System"] }, { ...

Read More

How to match and group array elements with the max value in MongoDB aggregation?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 680 Views

To match and group array elements with the maximum value in MongoDB aggregation, use $filter with $max to find elements with the highest score, then $group to count occurrences by name. Syntax db.collection.aggregate([ { "$project": { "maxElement": { "$arrayElemAt": [ ...

Read More

Implement MongoDB $addToSet for an array in an array and append a value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 430 Views

To implement MongoDB $addToSet for an array within an array and append a value, use the update() method with dot notation to target the specific nested array. The $addToSet operator adds a value to an array only if it doesn't already exist, preventing duplicates. Syntax db.collection.update( {query}, { $addToSet: { "parentArray.index.nestedArray": value } } ); Sample Data db.demo509.insertOne({ "value1": [ { ...

Read More
Showing 161–170 of 1,348 articles
« Prev 1 15 16 17 18 19 135 Next »
Advertisements