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
Check the current number of connections to MongoDB?
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 MoreMongoDB Query for boolean field as "not true
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 MoreMongoDB Query to select records having a given key?
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 MoreCommand to show the database currently being used in MongoDB?
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 MoreHow to clear console in MongoDB?
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 MoreHow to remove object from array in MongoDB?
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 MoreHow does MongoDB index arrays?
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 MoreMongoDB aggregation framework match OR is possible?
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 MoreMongoDB query condition on comparing 2 fields?
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 MoreHow to query for records where field is null or not set in MongoDB?
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