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
MongoDB Articles
Page 15 of 111
MongoDB Group query to get the count of repeated marks in documents?
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 MoreWhat does the max field mean in the output of MongoDB db..stats( )?
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 MoreGet fields from multiple sub-documents that match a condition in MongoDB?
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 MoreGet the maximum element in MongoDB collection?
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 MoreCreate ObjectId in MongoDB using a seed string?
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 MoreDisplay only the employee names with specific salaries in MongoDB documents with employee records?
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 MoreSearching for ranges in MongoDB?
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 MoreHow to work with Stored JavaScript in MongoDB?
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 MoreCount number of rows in a MongoDB collection
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 MoreHow would I filter out sub documents in MongoDB?
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