e-Zest members share technology ideas to foster digital transformation.

Basic commands for mongoDB

Written by Aarti Shinde | Sep 19, 2018 10:24:00 AM
1. Install MongoDB On Windows

To install MongoDB on Windows, first download the latest release of MongoDB from https://www.mongodb.org/downloads. Make sure you get correct version of MongoDB depending upon your Windows version.

2. Running mongoDB

Go to location where mongo DB is installed “C:\Program Files\MongoDB\Server\3.4\bin” and open command prompt at that location and execute following command in command prompt.


3. MongoDB Help


To get a list of commands, type db.help() in MongoDB client. This will give you a list of commands as shown in the following screenshot.
db.help


4. Show All Databases

Use below command to get list of all databases.
show dbs


5. Create new database

To create a new database execute the following command.
use DATABASE_NAME


6. Know your current selected database

To know your current working/selected database execute the following command
db


7. Drop database

To drop the database execute following command, this will drop the selected database
db.dropDatabase()


8. Create collection


To create the new collection execute the following commands
db.createCollection(name)


9. To check collections list

To get the list of collections created execute the following command
Show collections


10. Drop collection

To drop the selected collection execute the following command
db.COLLECTION_NAME.drop()


11. Insert document in collection
>db.COLLECTION_NAME.insert(document)

To insert single document in selected collection execute the following command


To insert multiple documents in selected collection execute following command


12. Get collection document

To get the list documents in collection execute the following command
>db.COLLECTION_NAME.find()


13. Update document

To update the document in collection execute the following command
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)


14. Save document

To save document in collection execute the following command
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})


15. Delete document

To delete document in selected collection execute the following command
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)


If you would like for more details on the content of this blog, please comment on the section given below and I would try my best to answer your all queries.