Database Articles

Page 14 of 547

MongoDB query (aggregation framework) to match a specific field value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 359 Views

To match a specific field value in MongoDB aggregation, use the $match operator. This operator filters documents based on specified criteria, similar to a find() query but within the aggregation pipeline. Syntax db.collection.aggregate([ { $match: { fieldName: "value" } } ]); Sample Data db.demo555.insertMany([ { "CountryName": "US" }, { "CountryName": "UK" }, { "CountryName": "US" }, { "CountryName": "AUS" }, { "CountryName": "US" } ]); { ...

Read More

Query MongoDB with "like" implementation on name and email field beginning with a specific letter?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 966 Views

To implement "like" functionality in MongoDB, use regular expressions with the /pattern/ syntax. For matching fields beginning with a specific letter, use the ^ anchor followed by the letter. Syntax db.collection.find({ "$or": [ { "fieldName1": /^letter/ }, { "fieldName2": /^letter/ } ] }); Sample Data db.demo554.insertMany([ { "UserName": "John", "UserMailId": "John@gmail.com" }, { "UserName": "Chris", "UserMailId": "Chris@gmail.com" }, ...

Read More

How to select documents with values above the average in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 511 Views

To select documents with values above the average in MongoDB, use the aggregation pipeline with $avg to calculate the average, then filter documents using $expr and $gt operators. Syntax db.collection.aggregate([ { $match: { $expr: { $gt: [ ...

Read More

MongoDB multiple OR conditions on same key?

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

To apply multiple OR conditions on the same field in MongoDB, use the $or operator with an array of condition objects. Each condition can target the same field with different values or operators. Syntax db.collection.find({ $or: [ { "fieldName": "value1" }, { "fieldName": "value2" }, { "fieldName": { $operator: "value3" } } ] }); Sample Data db.demo551.insertMany([ ...

Read More

Perform multiple updates with bulk operations and update elements in an array in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 364 Views

To perform multiple updates with bulk operations and update elements in an array in MongoDB, use initializeOrderedBulkOp(). It initializes and returns a new Bulk() operations builder for a collection. The builder constructs an ordered list of write operations that MongoDB executes in bulk. Syntax var bulk = db.collection.initializeOrderedBulkOp(); bulk.find({query}).updateOne({$set: {field: value}}); bulk.execute(); Sample Data Let us create a collection with documents: db.demo550.insertOne({ "Name": "Chris", "details": [ {"Marks": 49, "Result": "fail"}, ...

Read More

MongoDB concurrent update with sub collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 541 Views

To perform concurrent updates with sub collections in MongoDB, use the update() method combined with the $push operator and dot notation to reach nested fields within embedded documents and arrays. Syntax db.collection.update( { "field": "value" }, { $push: { "parentField.subField.arrayField": newValue } } ); Create Sample Data Let us create a collection with documents ? db.demo547.insertOne({ Name: "Chris", Test: { "FirstTest": { ...

Read More

Using $redact in MongoDB aggregate?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 700 Views

The $redact stage in MongoDB aggregation pipeline restricts document contents based on information stored within the documents themselves. It uses conditional logic to decide whether to keep, prune, or descend into document fields. Syntax db.collection.aggregate([ { $redact: { $cond: { if: , ...

Read More

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 738 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 547 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
Showing 131–140 of 5,468 articles
« Prev 1 12 13 14 15 16 547 Next »
Advertisements