We have installed Robo3T (flexible GUI for Mongodb, in our previous tutorial). Today in this tutorial we will see some database operations in MongoDB. So let’s get started with the database operations in MongoDB.
1) Open a new command prompt.
2) Enter “ mongod
” and then press ENTER. This is going to run mongodb in the background and we wait for connections on port 27017 (See in the below figure).
Now MongoDB server is running , leave that command prompt window open. If we close that Window then Mongodb server will get close.
3) Now open Robo3T.
4) Click on the computer icon on the left hand side below the menu bar (or simply press CTRL + N). A Mongodb connections window will popup.
5) Click on Connect.
6) After the connection is successful, you will find some databases on the left hand side.
You are ready to execute mongodb commands.
MongoDB Database Structure
Before we get started with the Mongodb Commands, let us just take a quick explanation with the structure of the database in Mongodb.
We have a Database, each database has related data called Collections, and each collections has the actual data called Documents. So a Database has one or more Collections and then each Collection has one or more Documents.
MongoDB Shell
You have to write mongodb commands in a shell. Robo3T already provides it.
Steps to open a shell:
1) Right click on the name of your connection that you have used while making a connection request (after the step 4).
2) Click on Open shell.
It will look like these. All commands have to be written in the shell(black box type structure).
Some basic Commands in MongoDB
1) How to Create a database : The command is “ use database-name
”.
Example: Write “ use testDB
” in the shell and press CTRL+ENTER.
If testDB doesn’t exists , it will automatically create a database testDB. If it is already exists it just switch it to testDB. It means that now all the upcoming operations will be executing in the database testDB.
2) To check which database we are working with: The command is “ db
”.
Example: Write “ db
” in the shell and press CTRL+ENTER. It will display the name testDB.
3) To list all the databases we have: The command is “ show dbs
”.
Example: Write “ show dbs
”, and then press CTRL + ENTER(it will show all the database in your system).
Note: local, admin, config are the default databases.
Collections and Documents in MongoDB
We have created a new database testDB with our first command. But after executing “
show dbs
” command we are not seeing the testDB database. In Mongo, we need to insert a document in that database for that database to be listed.
You have to make a new collection before inserting a document in the database.
Steps for making a Collection and inserting a Document:
1) To make a new Collection in the database : The command is ” db.createCollection (“collection-name”)
“.
(db means the database that we are using. Here db is the testDB)
Example: In the shell write “ db.createCollection("testCollection")
” and press CTRL+ENTER. It will create a collection testCollection in the testDB database(Output will be OK).
2) To insert a document in database : The command is ” db.collectionName.insert({“x”:”y”})
“.
Example: Write “db.testCollection.insert({"Name":"Car"})
“and press CTRL+ENTER. It will insert a document with key as Name and value as Car with the collection testCollection in testDB database.
(Output will be inserted 1 record(s)).
Now write “ show dbs
” in the shell and press CTRL+ENTER. You will now see the testDB database that you have created above.
How to delete a database: The command is “db.dropDatabase()
“. It will delete the current database that we are using.
Example: Write db.dropDatabase()
in the shell and press CTRL+ENTER. It will delete the current database that is testDB.(Output is OK)
It will delete the testDB .
Now write “ show dbs
” in the shell and press CTRL+ENTER. You will not now see the testDB database that you have created above.
If you have any queries regarding database operations in MongoDB, drop a comment below.