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
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
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
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
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
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
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
For large MongoDB collections with slow search performance, create indexes on frequently queried fields. Use the createIndex() method to optimize query execution time significantly. Syntax db.collection.createIndex({ field: 1 }); db.collection.createIndex({ field1: 1, field2: -1 }); Create Index and Sample Data First, create an index on the field you'll search frequently: db.demo661.createIndex({ListOfName: 1}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } Insert sample documents into the collection: db.demo661.insertMany([ ... Read More
To select the maximum item in each group with MongoDB, use the $group aggregation stage with the $max operator. This groups documents by a field and finds the maximum value within each group. Syntax db.collection.aggregate([ { $group: { _id: { fieldName: "$fieldName" }, maxField: { $max: "$valueField" } } ... Read More
To push and slice multiple times in MongoDB, use the $push operator with $each and $slice modifiers. The $slice operator limits the array size, keeping only the specified number of elements after each push operation. Syntax db.collection.update( { "field": "value" }, { "$push": { "arrayField": { "$each": ["newElement"], ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance