Database Articles

Page 106 of 547

Check the current number of connections to MongoDB?

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

To check the current number of connections to MongoDB, you can use the db.serverStatus().connections command. This returns detailed connection statistics including current, available, and total connections created. Syntax db.serverStatus().connections; Alternative syntax using a variable ? var connectionInfo = db.serverStatus(); connectionInfo.connections; Method 1: Direct Command The simplest way to check connection statistics ? db.serverStatus().connections; { "current" : 1, "available" : 999999, "totalCreated" : 1 } Method 2: Using a Variable Store the server status and access connection data ? var checkCurrentNumberOfConnections ...

Read More

MongoDB Query for boolean field as "not true

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

You can use the $ne (not equal) operator to query for boolean fields that are "not true". This operator returns documents where the field value is either false or does not exist. Syntax db.collectionName.find({ fieldName: { $ne: true } }); Sample Data Let us create a collection with employee documents to demonstrate the query ? db.queryForBooleanFieldsDemo.insertMany([ { "EmployeeName": "Larry", "EmployeeAge": 24, "isOldEmployee": true }, { "EmployeeName": "Mike", "EmployeeAge": 20, "isOldEmployee": false }, { "EmployeeName": "Sam", "EmployeeAge": ...

Read More

MongoDB Query to select records having a given key?

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

To select records having a given key, you can use the $exists operator. This operator checks whether a field exists in the document, regardless of its value. Syntax db.collectionName.find({ fieldName: { $exists: true } }); Sample Data Let's create a collection with sample documents to demonstrate the $exists operator ? db.selectRecordsHavingKeyDemo.insertMany([ {"StudentName": "John", "StudentAge": 21, "StudentMathMarks": 78}, {"StudentName": "Carol", "StudentMathMarks": 89}, {"StudentName": "Sam", "StudentAge": 26, "StudentMathMarks": 89}, {"StudentName": "Sam", "StudentMathMarks": 98} ]); { ...

Read More

Command to show the database currently being used in MongoDB?

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

The command to show the database currently used in MongoDB is the following − Syntax db; This command returns the name of the currently active database in your MongoDB session. Check Available Databases Let us first check how many databases are present. The query is as follows − show dbs; The following is the output displaying all the databases − admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB studentSearch 0.000GB test 0.003GB Example 1: Check Current Database Now, we have the list ...

Read More

How to clear console in MongoDB?

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

To clear the console in MongoDB shell, you can use either a keyboard shortcut or the cls command. Both methods instantly clear the terminal display, giving you a clean workspace. Syntax Method 1: Keyboard Shortcut Ctrl + L Method 2: Command cls Method 1: Using Ctrl + L (Recommended) The fastest way to clear the MongoDB console is using the keyboard shortcut. Simply press Ctrl + L while in the MongoDB shell ? > use myDatabase ...

Read More

How to remove object from array in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

You can use $pull operator to remove objects from an array in MongoDB. The $pull operator removes all array elements that match a specified query condition. Syntax db.collection.update( { "matchField": "value" }, { $pull: { "arrayField": { "field": "value" } } } ); Sample Data Let us create a collection with a document containing an array of objects ? db.removeObjectFromArrayDemo.insertOne({ "StudentName": "John", "StudentAcademicProjectDetails": [ { ...

Read More

How does MongoDB index arrays?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 226 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 294 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 464 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 828 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
Showing 1051–1060 of 5,468 articles
« Prev 1 104 105 106 107 108 547 Next »
Advertisements