Found 1349 Articles for MongoDB

Check the current number of connections to MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

696 Views

You can check the current number of connections to MongoDB with the help of the following syntax −var anyVariableName= db.serverStatus(); yourVariableName.connections;The second syntax is as follows −db.serverStatus().connections;To understand both the above syntaxes, let us see them one by one −Case 1 − The first query is as follows −> var checkCurrentNumberOfConnections = db.serverStatus() > checkCurrentNumberOfConnections.connections;The following is the output −{ "current" : 1, "available" : 999999, "totalCreated" : 1 }Case 2 − The second query is as follows −> db.serverStatus().connectionsThe following is the output −{ "current" : 1, "available" : 999999, "totalCreated" : 1 }

MongoDB Query for boolean field as “not true”

Smita Kapse
Updated on 30-Jul-2019 22:30:25

777 Views

You can use $ne(not equal) operator for this. The syntax is as follows −db.yourCollectionName.find({yourFieldName: {$ne: true}}).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"Larry", "EmployeeAge":24, "isOldEmployee":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7f7680f10143d8431e13") } > db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"Mike", "EmployeeAge":20, "isOldEmployee":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7f8680f10143d8431e14") } > db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"Sam", "EmployeeAge":23, "isOldEmployee":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7f9380f10143d8431e15") } > db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"David", "EmployeeAge":25, "isOldEmployee":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7fa280f10143d8431e16") } > db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"Carol", "EmployeeAge":27, "isOldEmployee":true}); ... Read More

MongoDB Query to select records having a given key?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

111 Views

To select records having a given key, you can use $exists operator. The syntax is as follows −db.yourCollectionName.find( { yourFieldName: { $exists : true } } ).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"John", "StudentAge":21, "StudentMathMarks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7be780f10143d8431e0f") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Carol", "StudentMathMarks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7bfc80f10143d8431e10") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Sam", "StudentAge":26, "StudentMathMarks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7c1280f10143d8431e11") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Sam", "StudentMathMarks":98}); {    "acknowledged" : true, ... Read More

Command to show the database currently being used in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

444 Views

The command to show the database currently used in MongoDB is the following −db;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.003GBNow, we have the list of all databases. Let us use the above syntax to check current database. The query is as follows −> db;The following is the output −sampleLook at the above sample output, we are currently using ‘sample’ database. Let us switch the database and verify again ... Read More

How to clear console in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

776 Views

To clear console in MongoDB, you can use any of the following two syntaxes.The first syntax is as follows, which is the usage of keyboard shortcut −Ctrl + LAfter pressing the above key, you can clear console in MongoDB.The second syntax is as follows −clsTo understand the above syntaxes, let us implement them one by one. Here is the snapshot of my console.The first query is as follows to clear console in MongoDB −Ctrl+L;The following is the output −Look at the above sample output, the console has been cleared. Let us check the console once again.The second query is as ... Read More

How to remove object from array in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can use $pull operator to remove the object from an array in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.removeObjectFromArrayDemo.insertOne( ... {    ...    ... "StudentName": "John",    ... "StudentAcademicProjectDetails":    ... [{          ... "StudentProjectId": 101,          ... "StudentProjectName": "Pig Dice Game"       ... },       ... {          ... "StudentProjectId": 110,          ... "StudentProjectName": "Library Management System"       ... ... Read More

How does MongoDB index arrays?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

113 Views

MongoDB indexes every value of an array so that you can query for single elements.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.indexingForArrayElementDemo.insertOne({"StudentFavouriteSubject":["MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8acdca6cea1f28b7aa0816") }Display all documents from a collection with the help of find() method. The query is as follows −> db.indexingForArrayElementDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8acdca6cea1f28b7aa0816"),    "StudentFavouriteSubject" : [       "MongoDB",       "MySQL"    ] }Here is the query by which MongoDB index ... Read More

MongoDB aggregation framework match OR is possible?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

158 Views

Let us first create a collection with the document. The query to create a collection with a document is as follows −> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"John",    "StudentLastName":"Smith", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3a96cea1f28b7aa080d") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Carol",    "StudentLastName":"Tayor", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3bc6cea1f28b7aa080e") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"David",    "StudentLastName":"Miller", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3ce6cea1f28b7aa080f") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Bob",    "StudentLastName":"Taylor", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3e16cea1f28b7aa0810") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Robert",    "StudentLastName":"Smith", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac3fb6cea1f28b7aa0811") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Mike",    "StudentLastName":"Miller", ... Read More

MongoDB query condition on comparing 2 fields?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

305 Views

To query condition on comparing 2 fields, use the following syntax −db.yourCollectionName.find( { $where: function() { return this.yourFirstFieldName < this.yourSecondFieldName } } ).pretty();To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.comparingTwoFieldsDemo.insertOne({"StudentName":"John", "StudentAge":21, "StudentMathMarks":99, "StudentPhysicsMarks":98}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac09e6cea1f28b7aa0807") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"Carol", "StudentAge":22, "StudentMathMarks":79, "StudentPhysicsMarks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac0b46cea1f28b7aa0808") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"David", "StudentAge":24, "StudentMathMarks":39, "StudentPhysicsMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ac0c96cea1f28b7aa0809") } > db.comparingTwoFieldsDemo.insertOne({"StudentName":"Bob", "StudentAge":23, "StudentMathMarks":87, "StudentPhysicsMarks":78}); {    "acknowledged" : ... Read More

How to remove an array element by its index in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

872 Views

You can remove an array element by its index using the following two steps −The first step is as follows −db.yourCollectionName.update({}, {$unset : {"yourArrayFieldName.yourIndexValue" : 1 }});The above syntax will put a null value at the location of ‘yourIndexValue’. After that, you need to pull the null value from array filed to remove from an array element.The second step is as follows −db.yourCollectionName.update({}, {$pull : {"yourArrayFieldName" : null}});To implement the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.removeArrayElementByItsIndexDemo.insertOne({"InstructorName":"David",    "InstructorAge":28, "InstructorSubject":["MongoDB", "MySQL", "Java", "SQL Server", ... Read More

Advertisements