Found 1359 Articles for MongoDB

How to stop MongoDB in a single command?

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

468 Views

In order to stop MongoDB in a single command, use the following syntax −mongo --eval "db.getSiblingDB('admin').shutdownServer()"Let us implement the above syntax in order to stop MongoDB in one command.First, use the shortcut key −Ctrl + C;The query is as follows −C:\Program Files\MongoDB\Server\4.0\bin>mongo --eval "db.getSiblingDB('admin').shutdownServer()"The following is the output − displaying the shutdown of the MongoDB server −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 [js] trying reconnect to 127.0.0.1:27017 failed 2019-03-14T21:56:11.331+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed 2019-03-14T21:56:11.333+0530 I QUERY [js] ... Read More

How to list all databases in the Mongo shell?

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

628 Views

To list all databases in the Mongo shell, you need to use show command. The syntax is as follows −show dbs;Let us implement the above syntax for MongoDB. The query is as follows −> show dbs;The following is the output −admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GBIf you create a new database in MongoDB then it won’t be present in list of databases.Let us check the same. Create a new database −> use studentTracker; switched to db studentTrackerNow list all the databases from Mongo shell. The query is as follows −> show dbs;The following is ... Read More

How to work Date query with ISODate in MongoDB?

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

2K+ Views

Use $gte operator along with ISODate() to work Date query with ISODate 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.dateDemo.insertOne({"StudentName":"John", "StudentAge":26, "AdmissionDate":new ISODate("2013-06-07")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a65799064dcd4a68b70ea") }Display all documents from a collection with the help of find() method. The query is as follows −> db.dateDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8a65799064dcd4a68b70ea"),    "StudentName" : "John",    "StudentAge" : 26,    "AdmissionDate" : ISODate("2013-06-07T00:00:00Z") }Here is the date query with ISODate in MongoDB ... Read More

How to find a document by the non-existence of a field in MongoDB?

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

59 Views

To find a document by the non-existence of a field in MongoDB, the syntax is as follows −db.yourCollectionName.find({ "yourFieldName" : { "$exists" : false } }).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.findDocumentNonExistenceFieldDemo.insertOne({"StudentName":"John", "StudentAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a5c629064dcd4a68b70e8") } > db.findDocumentNonExistenceFieldDemo.insertOne({"StudentName":"David", "StudentAge":26, "StudentMathMarks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a5c809064dcd4a68b70e9") }Display all documents from a collection with the help of find() method. The query is as follows −> db.findDocumentNonExistenceFieldDemo.find().pretty();The following is the output −{ ... Read More

Return query based on date in MongoDB?

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

277 Views

To return query based on the date in MongoDB, let us take an example.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.returnQueryFromDate.insertOne({"PassengerName":"John", "PassengerAge":23, "PassengerArrivalTime":new ISODate("2018-03-10 14:45:56")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a57be9064dcd4a68b70e4") } > db.returnQueryFromDate.insertOne({"PassengerName":"Larry", "PassengerAge":21, "PassengerArrivalTime":new ISODate("2018-05-19 11:10:23")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e5") } > db.returnQueryFromDate.insertOne({"PassengerName":"Mike", "PassengerAge":24, "PassengerArrivalTime":new ISODate("2018-08-25 16:40:12")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e6") } >db.returnQueryFromDate.insertOne({"PassengerName":"Carol", "PassengerAge":26, "PassengerArrivalTime":new ISODate("2019-01-29 09:45:10")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e7") }Display all documents from a ... Read More

How to delete everything in a MongoDB database?

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

368 Views

You can delete everything in a MongoDB database using dropDatabase() function. The syntax is as follows −use yourDatabaseName; db.dropDatabase();The above syntax will delete everything in a MongoDB database.To delete everything in a MongoDB database, let us first display all the databases from MongoDB. The query is as follows −> show dbsThe following is the output −use yourDatabaseName; admin 0.000GB config 0.000GB flighInformation 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GBNow we will delete everything from the database ‘flightInformation’.First, you need to switch the database to ‘flightInformation’. The query is as follows −> use flighInformation; switched to db flighInformationNow here ... Read More

Prettyprint in MongoDB shell as default?

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

140 Views

You can call pretty() function on cursor object to prettyprint in MongoDB shell. The syntax is as follows −db.yourCollectionName.find().pretty();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.prettyDemo.insertOne({"ClientName":"Larry", "ClientAge":27, "ClientFavoriteCountry":["US", "UK"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a440de01f572ca0ccf5f2") } >db.prettyDemo.insertOne({"ClientName":"Mike", "ClientAge":57, "ClientFavoriteCountry":["AUS", "UK"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a4420e01f572ca0ccf5f3") }Display all documents from a collection with the help of find() method. The query is as follows −> db.prettyDemo.find();The following is the output −{ "_id" : ObjectId("5c8a440de01f572ca0ccf5f2"), "ClientName" : "Larry", "ClientAge" ... Read More

How to list all collections in the Mongo shell?

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

7K+ Views

To list all collections in Mongo shell, you can use the function getCollectionNames().The syntax is as follows −db.getCollectionNames();You can use another command which is collections. The syntax is as follows −show collections;To list all collections in Mongo, use the above two functions. The query is as follows −> db.getCollectionNames();The following is the output −[    "ConvertStringToDateDemo",    "IdUpdateDemo",    "ProductsInformation",    "addFieldDemo",    "addNewFieldToEveryDocument",    "arrayInnerElementsDemo",    "arrayLengthGreaterThanOne",    "arrayOfArraysDemo",    "caseInsensitiveDemo",    "changeDataType",    "changeType",    "charactersAllowedDemo",    "charactersDemo",    "checkFieldContainsStringDemo",    "checkSequenceDemo",    "combinationOfArrayDemo",    "conditionalSumDemo",    "convertStringToNumberDemo",    "copyThisCollectionToSampleDatabaseDemo",    "countDemo",    "createSequenceDemo",    "distinctCountValuesDemo",    "distinctRecordDemo", ... Read More

How do I drop a MongoDB database from the command line?

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

222 Views

To drop a MongoDB database from the command line, use the following syntax:mongo yourDatabaseName --eval "db.dropDatabase()"To understand the above syntax, let us display all the database from MongoDB. The query is as follows −> show dbs;The following is the output −StudentTracker 0.000GB admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB test 0.003GBDrop the database with the name ‘StudentTracker’. The query is as follows to drop a database from command line −C:\Program Files\MongoDB\Server\4.0\bin>mongo StudentTracker --eval "db.dropDatabase()"The following is the output −MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/StudentTracker?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("afc34e93-b4c0-46f0-85bd-b80ed17b8c11") } MongoDB server version: 4.0.5 { "dropped" ... Read More

How to count number of distinct values per field/ key in MongoDB?

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

358 Views

You can use the distinct command for this. 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.distinctCountValuesDemo.insertOne({"StudentFirstName":"John", "StudentFavouriteSubject":["C", "C++", "Java", "MySQL", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a39f193b406bd3df60e07") } > db.distinctCountValuesDemo.insertOne({"StudentFirstName":"Larry", "StudentFavouriteSubject":["MongoDB", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a3a1193b406bd3df60e08") }Display all documents from a collection with the help of find() method. The query is as follows −> db.distinctCountValuesDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8a39f193b406bd3df60e07"),    "StudentFirstName" : "John",    "StudentFavouriteSubject" : [ ... Read More

Advertisements