<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">

Basic commands for mongoDB

Aarti Shinde Sep 19, 2018

MongoDB For Developers

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.

mongo.exe
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

mongo.exe1
4. Show All Databases

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

db.version
5. Create new database

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

6. Know your current selected database

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

mongo.exe3
7. Drop database

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

mongo.exe4

8. Create collection


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

mongo.exe5
9. To check collections list

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

mongo.exe6
10. Drop collection

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

mongo.exe7

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

To insert single document in selected collection execute the following command

mongo.exe8
To insert multiple documents in selected collection execute following command

mongo.exe9
12. Get collection document

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

mongo.exe10
13. Update document

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

mongo.exe11
14. Save document

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

mongo.exe12
15. Delete document

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

mongo.exe13
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.

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.