Articles on Trending Technologies

Technical articles with clear explanations and examples

Create ObjectId in MongoDB using a seed string?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 449 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 185 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 507 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 906 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 308 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

MongoDB large collection and slow search? How to fix?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 485 Views

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

How to select maximum item in each group with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

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

Push and slice multiple times in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

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

How to clear a MongoDB database?

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

To clear a MongoDB database completely, use the dropDatabase() method. This permanently removes the database and all its collections, documents, and indexes. Syntax use yourDatabaseName; db.dropDatabase(); Example: Clear a MongoDB Database First, let us show all existing databases ? show dbs MyDB 0.000GB admin 0.000GB config 0.000GB local 0.000GB onlinecustomertracker 0.000GB test 0.006GB Now, switch to the database you want to delete and drop it ? use onlinecustomertracker db.dropDatabase(); switched to ...

Read More
Showing 22911–22920 of 61,297 articles
Advertisements