MongoDB Articles

Page 4 of 111

Match MongoDB documents with field value greater than a specific number and fetch them?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

To match MongoDB documents with field values greater than a specific number, use the $gt operator in your query. This operator filters documents where the specified field value exceeds the given threshold. Syntax db.collection.find({ "fieldName": { "$gt": value } }); // Using with aggregation pipeline db.collection.aggregate([ { $match: { "fieldName": { "$gt": value } } } ]); Sample Data db.demo730.insertMany([ { "Name": "Chris", "Marks": 33 }, { "Name": "David", "Marks": 89 }, { "Name": "Chris", "Marks": ...

Read More

Mass insertion in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 173 Views

For mass insertion in MongoDB, use the insertMany() method to insert multiple documents into a collection in a single operation. This is more efficient than inserting documents individually using multiple insert() calls. Syntax db.collection.insertMany([ { document1 }, { document2 }, { document3 } ]); Example Let us create a collection with multiple bank customer documents ? db.demo729.insertMany([ { BankName: "HDFC Bank", cardType: "Credit", "CustomerName": [{Name: "Chris", Age: 25}] }, { BankName: "ICICI ...

Read More

Find MongoDB records with Price less than a specific value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 421 Views

To find MongoDB records with Price less than a specific value, use the $lt (less than) comparison operator. This operator filters documents where the specified field value is less than the given value. Syntax db.collection.find({ "field": { $lt: value } }); Sample Data Let us create a collection with sample price documents ? db.demo728.insertMany([ {Price: 75}, {Price: 59}, {Price: 79}, {Price: 89} ]); { "acknowledged": ...

Read More

MongoDB query to search for string like "@email" in the field values

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

To search for strings containing "@email" in MongoDB field values, use regular expressions with the find() method. The i flag enables case-insensitive matching. Syntax db.collection.find({"fieldName": /pattern/flags}); Sample Data Let us create a collection with documents − db.demo727.insertMany([ {UserId: "John@email.com"}, {UserId: "John@yahoo.com"}, {UserId: "Chris@EMAIL.com"} ]); { "acknowledged" : true, "insertedIds" : [ ObjectId("5eab375f43417811278f5898"), ObjectId("5eab376043417811278f5899"), ObjectId("5eab376143417811278f589a") ...

Read More

How can I count the documents in an array based on the value of a specific field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 196 Views

To count documents in an array based on the value of a specific field, use the $unwind operator to flatten the array, followed by $match to filter, and $group to count the matching elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.fieldName": "targetValue" } }, { $group: { _id: null, count: { $sum: 1 } }} ]); Sample Data ...

Read More

Set filtering conditions for nested array in MongoDB

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

To set filtering conditions for nested arrays in MongoDB, use the $filter operator combined with $anyElementTrue and $map in an aggregation pipeline. This approach allows you to filter outer array elements based on conditions within deeply nested arrays. Syntax db.collection.aggregate([ { $addFields: { "arrayField": { $filter: { ...

Read More

How to count a cursor's iteration in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

To count a cursor's iteration in MongoDB, you need to use custom logic with a while loop along with a find() cursor. This allows you to iterate through documents and count specific conditions during traversal. Syntax var cursor = db.collection.find(); var count = 0; while (cursor.hasNext()) { var document = cursor.next(); // Apply your counting logic here if (condition) { count++; } } Sample Data db.demo724.insertMany([ ...

Read More

Get count of array elements from a specific field in MongoDB documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 837 Views

To count array elements from a specific field in MongoDB, use the $size operator within an aggregation pipeline. This operator returns the number of elements in an array field for each document. Syntax db.collection.aggregate([ { $project: { count: { $size: "$arrayFieldName" } } } ]); Sample Data db.demo723.insertMany([ {"Subject": ["MySQL", "MongoDB"]}, {"Subject": ["C"]}, {"Subject": ["C++", "Java", "Python"]} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Count by multiple fields with MongoDB aggregation

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 879 Views

To count by multiple fields in MongoDB, use the $facet operator combined with $group and $sum. The $facet processes multiple aggregation pipelines within a single stage on the same set of input documents. Syntax db.collection.aggregate([ { $facet: { "pipeline1": [ /* aggregation stages */ ], "pipeline2": [ /* aggregation stages */ ] } ...

Read More

Update all the values of a field with a specific string in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

To update all the values of a field with a specific string in MongoDB, use the update() method with an empty filter and the multi: true option to affect all documents in the collection. Syntax db.collection.update( {}, { $set: { "fieldName": "newValue" } }, { multi: true } ); Sample Data db.demo720.insertMany([ { "SubjectName": "MySQL" }, { "SubjectName": "Java" }, { "SubjectName": "C" }, { ...

Read More
Showing 31–40 of 1,106 articles
« Prev 1 2 3 4 5 6 111 Next »
Advertisements