MongoDB Articles

Page 104 of 111

How does MongoDB index arrays?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 217 Views

MongoDB automatically indexes every value within an array field when you create an index on that field. This allows efficient querying of individual array elements without scanning the entire collection. Syntax db.collection.createIndex({"arrayField": 1}); db.collection.find({"arrayField": "specificValue"}); Sample Data Let us create a collection with a document containing an array field ? db.indexingForArrayElementDemo.insertOne({ "StudentFavouriteSubject": ["MongoDB", "MySQL"] }); { "acknowledged": true, "insertedId": ObjectId("5c8acdca6cea1f28b7aa0816") } Display the document to verify ? db.indexingForArrayElementDemo.find().pretty(); { ...

Read More

MongoDB aggregation framework match OR is possible?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 280 Views

Yes, MongoDB aggregation framework supports the $or operator within the $match stage to match documents that satisfy at least one of multiple conditions. This allows you to create flexible filtering logic in your aggregation pipelines. Syntax db.collection.aggregate([ { $match: { $or: [ { field1: value1 }, ...

Read More

MongoDB query condition on comparing 2 fields?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 454 Views

To query documents by comparing two fields in MongoDB, you can use the $where operator with JavaScript expressions or the more efficient $expr operator with aggregation expressions. Syntax // Using $where (JavaScript) db.collection.find({ $where: function() { return this.field1 < this.field2; } }); // Using $expr (Recommended) db.collection.find({ $expr: { $lt: ["$field1", "$field2"] } }); Sample Data db.comparingTwoFieldsDemo.insertMany([ { ...

Read More

How to query for records where field is null or not set in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 820 Views

In MongoDB, you can query for records where a field is null or not set using different operators. There are two distinct cases: when a field exists but is set to null, and when a field doesn't exist in the document at all. Syntax // Field exists and is null db.collection.find({"fieldName": null}); // Field does not exist db.collection.find({"fieldName": {$exists: false}}); // Field is null OR does not exist db.collection.find({$or: [{"fieldName": null}, {"fieldName": {$exists: false}}]}); Sample Data db.fieldIsNullOrNotSetDemo.insertMany([ {"EmployeeName": "Larry", "EmployeeAge": null, "EmployeeSalary": 18500}, ...

Read More

Find items that do not have a certain field in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 227 Views

To find documents that do not have a certain field in MongoDB, use the $exists operator with the value false. This operator checks whether a field is present in the document, regardless of its value. Syntax db.collection.find({"fieldName": {$exists: false}}) Sample Data Let's create a collection with documents where some have a specific field and others don't ? db.findDocumentDoNotHaveCertainFields.insertMany([ {"UserId": 101, "UserName": "John", "UserAge": 21}, {"UserName": "David", "UserAge": 22, "UserFavouriteSubject": ["C", "Java"]}, {"UserName": "Bob", "UserAge": 24, "UserFavouriteSubject": ["MongoDB", "MySQL"]} ]); ...

Read More

Performing regex Queries with PyMongo?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

PyMongo is a Python distribution containing tools for working with MongoDB. To perform regex queries with PyMongo, you use the $regex operator to match documents where field values match a specified regular expression pattern. Syntax db.collection.find({ "fieldName": { "$regex": "pattern", "$options": "flags" } }) Sample Data Let us create a collection with sample documents ? db.performRegex.insertMany([ { ...

Read More

Does MongoDB getUsers() and SHOW command fulfil the same purpose?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 138 Views

Both getUsers() method and show users command can be used to list all users in MongoDB, but they serve the same fundamental purpose with slightly different output formats. Syntax Using getUsers() method: db.getUsers(); Using show command: show users; Method 1: Using getUsers() The getUsers() method returns user information as an array of documents ? db.getUsers(); [ { "_id" : "test.John", "user" : "John", ...

Read More

How to stop MongoDB in a single command?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 720 Views

To stop MongoDB in a single command, use the shutdown command with the --eval option to execute the shutdown operation directly from the command line without entering the MongoDB shell. Syntax mongo --eval "db.getSiblingDB('admin').shutdownServer()" Example: Stopping MongoDB Server Execute the shutdown command from your system's command prompt or terminal ? mongo --eval "db.getSiblingDB('admin').shutdownServer()" The output shows the MongoDB server shutting down ? MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("c0337c02-7ee2-45d9-9349-b22d6b1ffe85") } MongoDB server version: 4.0.5 server should be down... 2019-03-14T21:56:10.327+0530 I NETWORK ...

Read More

How to work Date query with ISODate in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

Use date comparison operators like $gte, $lt, and $between along with ISODate() to work with date queries in MongoDB. ISODate provides a standardized way to store and query dates in UTC format. Syntax db.collection.find({ "dateField": { "$gte": ISODate("YYYY-MM-DDTHH:mm:ssZ"), "$lt": ISODate("YYYY-MM-DDTHH:mm:ssZ") } }); Sample Data Let us create a collection with sample documents containing date fields ? db.dateDemo.insertMany([ { ...

Read More

Return query based on date in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 381 Views

To return query based on the date in MongoDB, use date range operators like $gte, $gt, $lte, and $lt with ISODate objects to filter documents by date fields. Syntax db.collection.find({ "dateField": { $gte: ISODate("YYYY-MM-DDTHH:mm:ssZ") } }); Sample Data Let us create a collection with passenger data containing arrival dates ? db.returnQueryFromDate.insertMany([ { "PassengerName": "John", "PassengerAge": 23, "PassengerArrivalTime": ISODate("2018-03-10T14:45:56Z") ...

Read More
Showing 1031–1040 of 1,106 articles
« Prev 1 102 103 104 105 106 111 Next »
Advertisements