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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Create 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 MoreMongoDB large collection and slow search? How to fix?
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 MoreHow to select maximum item in each group with MongoDB?
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 MorePush and slice multiple times in MongoDB?
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 MoreHow to clear a MongoDB database?
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