MongoDB Articles

Page 15 of 111

MongoDB Group query to get the count of repeated marks in documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 234 Views

To get the count of repeated marks in MongoDB documents, use the $group aggregation stage with $sum to count occurrences of each unique mark value. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } } ]) Sample Data db.demo676.insertMany([ ...

Read More

What does the max field mean in the output of MongoDB db..stats( )?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 134 Views

The max field in MongoDB's db..stats() output represents the maximum number of documents allowed in a capped collection. This field only appears for capped collections and shows the document limit set during collection creation. Syntax db.createCollection("collectionName", { capped: true, size: sizeInBytes, max: maxDocuments }); Create Sample Capped Collection Let's create a capped collection with a maximum of 50 documents ? db.createCollection("demo673", { capped: true, size: 100, ...

Read More

Get fields from multiple sub-documents that match a condition in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 732 Views

To get fields from multiple sub-documents that match a condition in MongoDB, use the aggregation pipeline with $unwind to flatten nested arrays, $match to filter documents, and $project to select specific fields. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.nestedField": "condition" } }, { $project: { fieldName: "$arrayField.targetField" } } ]); Sample Data db.demo671.insertOne({ "details": [ { ...

Read More

Get the maximum element in MongoDB collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 202 Views

To get the maximum element from a MongoDB collection, use the sort method with descending order combined with limit(1) to retrieve only the document with the highest value. Syntax db.collection.find().sort({"fieldName": -1}).limit(1); Sample Data db.demo669.insertMany([ {"Marks": 76}, {"Marks": 55}, {"Marks": 89}, {"Marks": 88}, {"Marks": 79} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5ea3133c04263e90dac943d9"), ...

Read More

Create ObjectId in MongoDB using a seed string?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 445 Views

MongoDB's ObjectId cannot directly accept a seed string, but you can create custom _id values using string identifiers instead of the default ObjectId format. This approach gives you control over document identification while maintaining uniqueness. Syntax db.collection.insertOne({_id: "stringValue"}); Create Sample Data Insert documents with custom string-based _id values ? db.demo667.insertMany([ {_id: "Chris"}, {_id: "David"}, {_id: "Bob"}, {_id: "Mike"} ]); { "acknowledged": true, "insertedIds": ["Chris", "David", "Bob", "Mike"] } ...

Read More

Display only the employee names with specific salaries in MongoDB documents with employee records?

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

To display only the employee names with specific salaries in MongoDB, use the $in operator to match multiple salary values and apply field projection to return only the employee names. Syntax db.collection.find( { "fieldName": { $in: [value1, value2, value3] } }, { _id: 0, "fieldToExclude": 0 } ); Sample Data db.demo666.insertMany([ { "EmployeeName": "John", "EmployeeSalary": 25000 }, { "EmployeeName": "Chris", "EmployeeSalary": 35000 }, { "EmployeeName": "David", "EmployeeSalary": 65000 }, ...

Read More

Searching for ranges in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

To search for ranges in MongoDB, use comparison operators like $gte, $lte, $gt, and $lt to find documents within specific value ranges. You can combine these with sort() and limit() for more control over results. Syntax db.collection.find({ field: { $gte: minValue, $lte: maxValue } }); Sample Data db.demo665.insertMany([ { "Value": 10 }, { "Value": 15 }, { "Value": 55 }, { "Value": 25 }, { "Value": 20 } ]); ...

Read More

How to work with Stored JavaScript in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 501 Views

MongoDB allows you to store JavaScript functions in the system.js collection for reuse across database operations. These stored functions can be saved using db.system.js.save() and executed with db.eval(). Syntax db.system.js.save({ _id: "functionName", value: function (parameters) { // Function logic here return result; } }); Example: Creating a Stored Function Let's create a function that formats a return message ? db.system.js.save({ ...

Read More

Count number of rows in a MongoDB collection

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 898 Views

To count the number of documents in a MongoDB collection, use the countDocuments() method. This method returns the total number of documents that match the specified query criteria. Syntax db.collection.countDocuments(query, options) Where query is optional. If omitted, it counts all documents in the collection. Sample Data Let us create a collection with sample documents ? db.demo664.insertMany([ {_id: 1, ClientName: "Chris"}, {_id: 2, ClientName: "Bob"}, {_id: 3, ClientName: "Sam"}, {_id: 4, ClientName: "David"} ]); ...

Read More

How would I filter out sub documents in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 296 Views

To filter out sub documents in MongoDB, use the aggregation pipeline with $unwind to deconstruct array elements and $match to apply filtering conditions on the individual sub documents. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": { $operator: value } } } ]); Sample Data db.demo662.insertOne({ "details": [ { "Name": "Chris", ...

Read More
Showing 141–150 of 1,106 articles
« Prev 1 13 14 15 16 17 111 Next »
Advertisements