Database Articles

Page 16 of 547

Implement match and project in MongoDB aggregate

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

The $match operator filters documents to pass only those that match specified conditions to the next pipeline stage. The $project operator reshapes documents by including, excluding, or adding new fields before passing them to the next stage. Syntax db.collection.aggregate([ { $match: { field: value } }, { $project: { field1: 1, field2: 0, newField: "$existingField" } } ]); Sample Data Let us create a collection with student documents ? db.demo545.insertMany([ { "Name": ...

Read More

How to print NumberLong value in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 752 Views

In MongoDB, the NumberLong() wrapper handles 64-bit integers that exceed JavaScript's safe integer range. To print NumberLong values, use the toString() method or direct printing to display the value properly. Syntax var variableName = NumberLong("longNumber"); variableName.toString(); // Or direct printing print(variableName); Example 1: Using toString() Method Create a NumberLong variable and display it using toString() ? var number = NumberLong("231231231231121231"); number.toString(); NumberLong("231231231231121231") Example 2: Another NumberLong Value Display a different NumberLong value ? var anotherNumber = NumberLong("765765765765567576"); anotherNumber.toString(); NumberLong("765765765765567576") ...

Read More

Need to aggregate by hour and $avg in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 563 Views

To aggregate data by hour and calculate the average in MongoDB, use the aggregate() method with $group stage. The $hour operator extracts the hour from a date field, and $avg calculates the average value. Syntax db.collection.aggregate([ { $group: { "_id": { "$hour": "$dateField" }, "fieldName": { "$avg": "$numericField" } } ...

Read More

Find document that matches same array elements in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 457 Views

To find a document that matches the same array elements in MongoDB, use the $all operator combined with $size. The $all operator selects documents where the array field contains all specified elements, while $size ensures the array has exactly the required number of elements. Syntax db.collection.find({ "arrayField": { $all: ["element1", "element2", "element3"], $size: numberOfElements } }); Sample Data db.demo543.insertMany([ {id: 101, subject: ...

Read More

Add a boolean field true to returned objects, when a specified value is in array. For NULLnor other values, set false.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 293 Views

To add a boolean field true to returned objects when a specified value exists in an array (and false for NULL or missing values), use the $ifNull operator with $setIntersection and $size to check array membership. Syntax db.collection.aggregate([ { "$project": { "fieldName": { "$eq": [ ...

Read More

Select documents grouped by field in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 514 Views

To select documents grouped by field in MongoDB, use the $group stage in an aggregation pipeline along with $project to reshape the output. The $group stage groups documents by specified field values and accumulates data using operators like $push. Syntax db.collection.aggregate([ { "$group": { "_id": { "field1": "$field1", "field2": "$field2" }, "groupedData": { "$push": { "field3": "$field3" } } ...

Read More

MongoDB query to remove subdocument from document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 996 Views

To remove a subdocument from a document in MongoDB, use the $pull operator along with update(). The $pull operator removes all array elements that match a specified condition. Syntax db.collection.update( { "matchField": "value" }, { $pull: { "arrayField": { "field": "condition" } } } ); Create Sample Data db.demo538.insertOne({ id: 101, "details": { anotherDetails: [ { ...

Read More

Group with multiple fields and get the count of duplicate field values grouped together innMongoDB

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

To group documents with multiple fields and count duplicate combinations in MongoDB, use the aggregation framework with $project and $group stages. The $cond operator helps normalize field values for consistent grouping regardless of field order. Syntax db.collection.aggregate([ { $project: { field1: { $cond: { if: condition, then: value1, else: value2 } }, field2: { $cond: { if: condition, then: value1, else: ...

Read More

How do I delete array value from a document in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 562 Views

To delete array values from a document in MongoDB, use the $pull operator. The $pull operator removes from an existing array all instances of a value or values that match a specified condition. Syntax db.collection.update( { "matchCondition": "value" }, { $pull: { "arrayField": "valueToRemove" } } ); Create Sample Data Let us first create a collection with documents − db.demo535.insertOne({ "studentId": "101", "studentName": "Chris", "ListOfMailIds": [ ...

Read More

Unable to implement $addToSet in MongoDB to fetch values of a single field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 323 Views

The $addToSet operator adds values to an array unless the value is already present, in which case $addToSet does nothing to that array. When used with $group in aggregation, it creates a unique set of values from a field across multiple documents. Syntax db.collection.aggregate([ { $group: { _id: groupingField, arrayField: { $addToSet: "$fieldName" } ...

Read More
Showing 151–160 of 5,468 articles
« Prev 1 14 15 16 17 18 547 Next »
Advertisements