Database Articles

Page 15 of 547

MongoDB aggregate to get the count of field values of corresponding duplicate names?

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

To count field values for corresponding duplicate names in MongoDB, use the aggregation pipeline with $unwind, $group, and $project stages to flatten arrays, group by names, and calculate counts. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField.nameField", count: { $sum: 1 }, otherFields: { $push: "$arrayField.otherField" } }}, { $project: { ...

Read More

How to run MongoDB query to update only a specific field value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 264 Views

To update only a specific field value in MongoDB, use the $set operator with the update() method. This allows you to modify particular fields without affecting other document data. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "field": "newValue" } } ); Create Sample Data Let us create a collection with sample documents ? db.demo557.insertMany([ { Name: "Chris" }, { Name: "David" } ]); { "acknowledged": true, ...

Read More

Fetch records from a subdocument array wherein id begins from 234 in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 148 Views

To fetch records from a subdocument array where id begins with specific digits like "234", use MongoDB's aggregation pipeline with $unwind, $match, and $regex operators. The $unwind deconstructs the array, $regex filters by pattern, and $group with $push reconstructs matching elements. Syntax db.collection.aggregate([ { $match: { "_id": documentId } }, { $unwind: "$arrayField" }, { $match: { "arrayField.id": { $regex: /^pattern/ } } }, { $group: { _id: "$_id", "result": { $push: "$arrayField" } } } ]); Sample Data ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 369 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 980 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 525 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 378 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 555 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 710 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
Showing 141–150 of 5,468 articles
« Prev 1 13 14 15 16 17 547 Next »
Advertisements