Articles on Trending Technologies

Technical articles with clear explanations and examples

Understanding MongoDB Query Plan

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 608 Views

To understand how MongoDB executes queries, use the explain() method. Query plans show the execution strategy, including which indexes are used and performance statistics for query optimization. Syntax db.collection.explain().find(query); db.collection.explain("executionStats").find(query); db.collection.explain("allPlansExecution").find(query); Sample Data Let us create a collection with documents and an index ? db.demo408.insertMany([ {"Value": 50}, {"Value": 20}, {"Value": 45}, {"Value": 35} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

How to create MongoDB user on existing database with correct role?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 314 Views

To create a new user in MongoDB, use the createUser() method on an existing database. This allows you to assign specific roles and authentication mechanisms to control user permissions. Syntax db.createUser({ user: "username", pwd: "password", roles: [ { role: "roleName", db: "databaseName" } ], mechanisms: ["SCRAM-SHA-256"] }); Example: Create User with readWrite Role Create a user named "John" with readWrite permissions on the test database ? ...

Read More

How to use arrays as filters by querying subdocuments in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 174 Views

To use arrays as filters when querying subdocuments in MongoDB, use the $setIsSubset operator within an aggregation pipeline. This operator checks if one array is a subset of another, making it useful for filtering array elements based on specific values. Syntax db.collection.aggregate([ { $project: { "arrayField": { $filter: { input: "$arrayField", ...

Read More

Working with MongoDB find()

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 230 Views

The find() method in MongoDB selects documents in a collection or view and returns a cursor to the selected documents. It is the primary method for querying data from MongoDB collections. Syntax db.collection.find(query, projection) Parameters: query (optional): Specifies selection criteria using query operators projection (optional): Specifies which fields to return Sample Data Let us create a collection with sample documents ? db.demo405.insertMany([ {"StudentInfo": {"Name": "Chris"}}, {"StudentInfo": {"Name": "David"}}, {"StudentInfo": {"Name": "Bob"}}, {"StudentInfo": {"Name": ...

Read More

Transaction lock in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 732 Views

In MongoDB versions prior to 4.0, native transactions weren't available. To implement transaction-like locking behavior, you can use findOneAndUpdate() with a lock field to ensure atomic operations on documents. Syntax db.collection.findOneAndUpdate( { "lock_field": { "$exists": false } }, { "$set": { "lock_field": true } } ) Sample Data db.demo404.insertMany([ { "FirstName": "John" }, { "FirstName": "Robert" }, { "FirstName": "Mike" } ]); { "acknowledged": true, ...

Read More

MongoDB query to sort by words

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 251 Views

To sort by words in a custom order in MongoDB, use $addFields with $cond to assign priority values to specific words, then sort by those values. Syntax db.collection.aggregate([ { $addFields: { sortByWords: { $cond: [ { $eq: ...

Read More

How we can perform sort on ObjectId column in MongoDB?

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

To perform sort on ObjectId column in MongoDB, use the sort() method with the _id field. ObjectIds are sorted by their creation timestamp, making them useful for chronological ordering. Syntax db.collection.find().sort({_id: 1}); // Ascending (oldest first) db.collection.find().sort({_id: -1}); // Descending (newest first) Sample Data db.demo403.insertMany([ {"Name": "Chris"}, {"Name": "David"}, {"Name": "Bob"}, {"Name": "Adam"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

How to get the intersection of two arrays in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 977 Views

To get intersection of two arrays in MongoDB, use $setIntersection along with aggregate(). This operator returns an array containing elements that appear in both input arrays. Syntax db.collection.aggregate([ { $project: { fieldName: { $setIntersection: ["$array1", "$array2"] } } } ]); Sample Data Let us create a collection with documents ? db.demo61.insertOne({ "Values1": ...

Read More

MongoDB query to get date records in a range

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

To get date records in a range, use the $gt (greater than) and $lt (less than) operators together to define the date boundaries for your query. Syntax db.collection.find({ "dateField": { "$gt": ISODate("start-date"), "$lt": ISODate("end-date") } }); Sample Data db.demo60.insertMany([ {"ArrivalDate": new ISODate("2019-01-11 12:30:10")}, {"ArrivalDate": new ISODate("2019-10-12 03:10:00")}, {"ArrivalDate": new ISODate("2019-01-14 05:11:20")} ]); { ...

Read More

Set multiple conditions in MongoDB and fetch value in a range

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

To set multiple conditions in MongoDB and fetch values in a range, use comparison operators like $gt (greater than) and $lt (less than) combined in a single query. This allows you to filter documents based on numeric ranges or multiple criteria. Syntax db.collection.find({ field: { $gt: minValue, $lt: maxValue } }); Sample Data db.demo59.insertMany([ { "Values": 50 }, { "Values": 10 }, { "Values": 58 }, { "Values": 78 } ]); ...

Read More
Showing 23061–23070 of 61,297 articles
Advertisements