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
Database Articles
Page 14 of 547
MongoDB query (aggregation framework) to match a specific field value
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 MoreQuery MongoDB with "like" implementation on name and email field beginning with a specific letter?
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 MoreHow to select documents with values above the average in MongoDB?
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 MoreMongoDB multiple OR conditions on same key?
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 MorePerform multiple updates with bulk operations and update elements in an array in MongoDB
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 MoreMongoDB concurrent update with sub collection?
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 MoreUsing $redact in MongoDB aggregate?
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 MoreImplement match and project in MongoDB aggregate
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 MoreHow to print NumberLong value in MongoDB?
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 MoreNeed to aggregate by hour and $avg in MongoDB
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