Found 1359 Articles for MongoDB

Case insensitive search in Mongo?

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

2K+ Views

You can restrict case insensitive search in MongoDB with the help of '$regex'. The syntax is as follows −db.yourCollectionName.find({"yourFieldName" : { '$regex':'^yourValue$'}});You can use another regex. The syntax is as follows −db.yourCollectionName.find({"Name" : { '$regex':/^yourValue$/i}});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.caseInsesitiveDemo.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bd66293c80e3f23815e83") } > db.caseInsesitiveDemo.insertOne({"Name":"Johnson"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bd66693c80e3f23815e84") } > db.caseInsesitiveDemo.insertOne({"Name":"Johny"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bd66a93c80e3f23815e85") }Display all documents from a collection with ... Read More

Is it possible to write to MongoDB console in JavaScript execution?

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

75 Views

To write on console, you need to use print() method. The syntax is as follows −print(“yourString”);To display objects, you can use printjson(). The syntax is as follows −printjson(yourObjectName);Let us implement both the functions. The first query is as follows to display something −> print("Welcome to MongoDB Console");The following is the output on a console −Welcome to MongoDB ConsoleLet us create an object. The query is as follows −>studentInformation={"StudentName":"John", "StudentAge":24, "StudentTechnicalSkills":["C", "C++", "Java", "MongoDB", "MySQL"]}; {    "StudentName" : "John",    "StudentAge" : 24,    "StudentTechnicalSkills" : [       "C",       "C++",       "Java",     ... Read More

Clone a collection in MongoDB?

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

549 Views

To clone a collection in MongoDB, you can use the forEach() method. Let us first create a collection with a document.The query to create a collection with a document is as follows −> db.studentInformation.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bc15780f10143d8431e21") } > db.studentInformation.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bc15e80f10143d8431e22") } > db.studentInformation.insertOne({"StudentName":"James"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bc17380f10143d8431e23") }Display all documents from a collection with the help of find() method. The query is as follows −> db.studentInformation.find().pretty();The following is the output −{ "_id" : ObjectId("5c8bc15780f10143d8431e21"), "StudentName" : "Chris" } { "_id" ... Read More

How to get element with max id in MongoDB?

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

904 Views

To get the element with a max id, you can use the find() method. To understand the above concept, let us create a collection with the document. The query is as follows −> db.getElementWithMaxIdDemo.insertOne({"Name":"John", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbce480f10143d8431e1c") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Larry", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbcec80f10143d8431e1d") } > db.getElementWithMaxIdDemo.insertOne({"Name":"David", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbcf580f10143d8431e1e") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Chris", "Age":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbcfe80f10143d8431e1f") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Robert", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbd0880f10143d8431e20") }Display all documents from ... Read More

How can I rename a collection in MongoDB?

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

618 Views

To rename a collection in MongoDB, you can use renameCollection() method. The syntax is as follows −db.yourOldCollectionName.renameCollection('yourNewCollectionName');To understand the above syntax, let us list all the collections from database sample. The query is as follows −> use sample; switched to db sample > show collections;The following is the output −copyThisCollectionToSampleDatabaseDemo deleteDocuments deleteDocumentsDemo employee informationAboutDelete internalArraySizeDemo prettyDemo selectWhereInDemo sourceCollection updateInformation userInformationNow change collection name ‘informationAboutDelete’ to ‘deleteSomeInformation’. The query is as follows to change the collection name.> db.informationAboutDelete.renameCollection('deleteSomeInformation'); { "ok" : 1 }Here is the query to check the collection name has been renamed to 'deleteSomeInformation' −> show collections;The following is ... Read More

How to aggregate sum in MongoDB to get the total count?

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

782 Views

To aggregate sum in MongoDB to get the total count, you can use the $sum operator. To understand the above concept, let us create a collection with the document −> db.aggregateSumDemo.insertOne({"CustomerName":"Larry", "Amount":140}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8baa0680f10143d8431e18") } > db.aggregateSumDemo.insertOne({"CustomerName":"Mike", "Amount":160}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8baa1380f10143d8431e19") } > db.aggregateSumDemo.insertOne({"CustomerName":"Sam", "Amount":300}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8baa1c80f10143d8431e1a") } > db.aggregateSumDemo.insertOne({"CustomerName":"David", "Amount":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8baa2580f10143d8431e1b") }Display all documents from a collection with the help of find() method. The query is as follows −> db.aggregateSumDemo.find().pretty();The following ... Read More

Check the current number of connections to MongoDB?

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

658 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

731 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

105 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

418 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

Advertisements