Setting up MongoDB and an introduction to shell

In this post, we will install MongoDB in our local machine and we will also start working with the MongoDB shell.

Will will use the community edition of MongoDB which can be downloaded from here.

The installation process is quite simple; you can follow the steps mentioned on the official website.

Once installed successfully, set the path of the installation directory till bin folder in environment variables in windows and run mongo –version from the command prompt. Successful installation will display the following information.

MongoDB shell version v4.2.6
allocator: tcmalloc
modules: none
build environment:
distmod: 2012plus
distarch: x86_64
target_arch: x86_64

Do you know, WiredTiger is the default storage engine starting in MongoDB 3.2.

Done with configuration, open command prompt, and run mongo. If mongo db is up and running you will be connected to the Mongo shell-like below.

Mongo shell

If it failed to connect to the MongoDB and display an error as below, you need to start the MongoDB manually.

MongoDB shell version v4.2.6
connecting to: mongodb://
2020-05-28T11:41:46.879+0530 E QUERY Error: couldn't connect to server, connection attempt failed: SocketException: Error connecting to :: caused by :: No connection could be made because the target machine actively refused it. :
2020-05-28T11:41:46.882+0530 F - [main] exception: connect failed
2020-05-28T11:41:46.882+0530 E - [main] exiting with code 1

To start the Mongo go to the bin folder of the installation directory and run the mongod.exe. By default, MongoDB will create a data folder parallel to the bin folder to store the database detail.

Setting up MongoDB installation directory

However, you can specify a different folder to store both database and the logs by running the following command from the command prompt.

C:/Program Files/MongoDB/Server/4.2/bin/mongod --dbpath D:/mongodb/db --logpath D:/mongodb/logs/db.log

After running MongoDB successfully, let’s connect to the Mongo shell and get our hands dirty.

We will create a database and then we will insert some records using the following steps.

  1. Run use shop, to create a database shop. The use command will create a database if it does not exist otherwise it will return the existing one.
  2. Now run show dbs, to display the list of databases.
  3. Let’s insert our first record by running following command
  4. db.products.insertOne({name:”A book”,published:”10/11/2019″,price:300})
  5. Here products are the name of the collection to be created inside the shop database if it does not exist otherwise a new record will be inserted in the existing collection. You can verify it using the show collections command.

Here are all commands.

use shop
switched to db shop
> db.products.insertOne({name:"A book",publishedOn:"10/11/2019",price:300})
"acknowledged" : true,
"insertedId" : ObjectId("5ecf642b40f67acafaf1f1f4")

> show collections

Notice the highlighted text above. That is the output of the insert statement, and it returns acknowledgment true and the Id of the newly created document. You can verify it using the find command.

"_id" : ObjectId("5ecf641640f67acafaf1f1f3"),
"name" : "A book",
"publishedOn" : "10/11/2019",
"price" : 300

That’s all in this post about MongoDB installation and introduction to Mongo shell.

Happy Learning !!

Leave a Comment