Learn MongoDB Step By Step

MongoDB

Before learning how to install and use MongoDB, let's understand the basic definition of it.

Definitions

MongoDB is open source, cross-platform and NoSQL database. Ha, you may think now what is NoSQL database? A NoSQL database means it does not have any relationships (non-relational). It is a document-oriented database.

Where to download

Download for Servers.

https://www.mongodb.com/download-center#community

MongoDB

Download for personal computers

From the below URL, you can download MongoDB for all versions of Windows computer.

https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl?_ga=1.68020441.930705794.1487476524

MongoDB

Installation of MongoDB server

Installing MongoDB.

MongoDB

After installing MongoDB, the next step is to set up MongoDB environment.

Set up the MongoDB environment

In the below snapshot, I have shown the folder where my MongoDB is installed after installing it.

Note - You can choose your folder location as per your requirement.

MongoDB

MongoDB requires a data directory to store all the data. If we have Installed .msi files in C drive, then just create data >> db folders in C drive.

MongoDB

After creating a folder, just copy the path of the folder where you have installed MongoDB. In my case, I have installed it in C drive, and got the path of the mongod.exe file.

Path

[C:\Program Files\MongoDB\Server\3.2\bin]

MongoDB

Starting Server of MongoDB

In the next step, open your Command Prompt as Administrator and get into directory where mongod.exe file is stored [C:\Program Files\MongoDB\Server\3.2\bin].

Note - If your computer runs on x64 bit, then you do not specify storageEngine “--storageEngine=mmapv1”.  

Then, type mongod.exe with “--storageEngine=mmapv1” and press Enter button.

After pressing Ente, the MongoDB Server will start to listen on port 27017.

MongoDB

And all your data files of MongoDB will be stored in [C:\data\db].

MongoDB

Alternate path

If you want to store data files in a different drive, then you need to follow the below process.

Create a new folder in another driver [/data/db] (in my case, it is E drive).

You can specify an alternate path for data files using the --dbpath option to mongod.exe.

MongoDB

In next step, open your Command Prompt as Administrator.

Then, get into the directory where mongod.exe file is stored [C:\Program Files\MongoDB\Server\3.2\bin].

MongoDB

Note - If your computer runs on x64 bit, then you do not specify storageEngine “--storageEngine=mmapv1”.

Now, to specify an alternate path for storing data files, use --dbpath option to mongod.exe.

MongoDB

After entering --dbpath path, just press Enter.

MongoDB

The MongoDB Server will start listening on port 27017 and all your data files of MongoDB will be stored in [E:\data\db].

Now, we are done with installation and starting MongoDB Server. In next step, we are going to connect MongoDB to Mongo.exe.

Connect to MongoDB

The first step of connecting is to go to folder where your mongo.exe is stored in my case [C:\Program Files\MongoDB\Server\3.2\bin]. In next step, open your Command Prompt as Administrator.

Then, get into the directory where mongo.exe file is stored [C:\Program Files\MongoDB\Server\3.2\bin].

MongoDB

After entering the path of mongo.exe files, just type mongo.exe and press Enter to connect.

In the below snapshot, we are connected to MongoDB.

MongoDB

Source

https://www.mongodb.com

MongoDB

After connecting to MongoDB, we are going to learn how to create a database of MongoDB from the command prompt (CLI).

Creating MongoDB Database.

MongoDB

For creating Database in MongoDB, just type the following command:

 Use [Database Name]

Let’s create a database with name demodatabase. So, type the following command.

MongoDB

Now, we have created demodatabase Database.

See List Databases

If we want to see created Database, then we need to enter

show dbs
 
MongoDB
MongoDB

Oops, we are not able to see database. To see the database, we need to create a collection [Collection in Mongo means Table in SQL] in this database.

Creating Collection

MongoDB

Next step, we are going to create collection in Database with name “employee”.

MongoDB

Now, we have created Collection. Let’s run command “show dbs” to see a list of the databases we have by now.

MongoDB

Wow, we have 2 databases and we can see database which we have created.

Drop Database in MongoDB

MongoDB

To drop database which we have created, just type command

db.dropDatabase()
 
MongoDB

Wow, we have created a database and also we have dropped it.

Note

Create demodatabase Database if you have drop demodatabase for further demos.

Insert Document

 Basic understanding if you know SQL [Document == Row and Collection== Tables] in MongoDB.

 Inserting Document in MongoDB Collection as we insert row in SQL server table

 In this part, we are simply going to insert Name and Profession of the employee.

Definition

MongoDB
MongoDB

See List of Insert Document

 In Mongo DB if you want to see all list of document in particular collection then you need to enter command

“db.[Collection Name].find()”

In this part, we are simply going to see a list of employees which we have inserted.

Definition

MongoDB
MongoDB

Note

What is an employee? Answer: - it is a collection which we have created.

Search Document by field (column)

In Mongo DB if you want to search a single document in particular collection then we can use findOne Method.

In this part, we are simply going to search employee by name, and for doing that we need specific selection criteria in findOne Method.

Definition

MongoDB
MongoDB

Delete Document

Using DeleteOne Method (To Delete Single Document)

Delete at most a single document that matches a specified filter even though multiple documents may match the specified filter.

MongoDB

In this part, we are going to delete employee document using deleteOne Method, for deleting document we also need to specify a filter and if the filter gets more than one record then the first document that matches the filter will be deleted.

Note

Deletes the first document that matches the filter.

MongoDB

Using remove Method (To Delete Single Document)

Delete a single document or all documents that match a specified filter.

MongoDB

If we want to remove single document then we need to use particular id (Objectid which is a unique id in MongoDB) or another unique field.

We are going to use Objectid (Objectid is unique id) for deleting a particular employee document.

MongoDB

Using remove Method for Deleting document using Unique Field other than Objectid

Delete document using remove method but this time we are going to use another unique field (“Name”) for removing the document.

MongoDB
MongoDB

Deleting all Document

  1. Using Remove Method(for removing all documents)
    For Deleting all documents we do not need to pass any filter just pass empty document ({ }) as shown in below syntax.

    MongoDB

    We just need to pass Collection name and then remove method with empty document ({ }).

    MongoDB
  1. Using Deleting Many Method
    Removes all documents that match the filter from a collection

    In this part we are going to delete employee based on Profession, we have 3 employees of student professions which we are going to delete.

    MongoDB
    MongoDB

Updating Document

Updates an existing document or documents in a collection, by default update document, update the only single document.

For updating documents we first need to pass the selection criteria for the update (means a unique id or field on which we can select fields to update) then after that, we need to pass data to update (document).

In this part we are going to update employee details based on Object id which we are going to pass in selection criteria then in the $set we are going to update employee Profession from “PL SQL Developer” to “Node.js Developer”.

Note

  1. The $set operator replaces the value of a field with the specified value.
  2. To update multiple documents set the Multi parameter to true by default it is false in options

  3. MongoDB
    MongoDB

Using updateOne Method (To update only single document)

Updates a single document within the collection based on the filter.

For updating document we first need to pass the selection criteria for the update (means a unique id or field on which we can select fields to update) then after that, we need to pass data to update (document).

In this part we are going to update employee details based on Object id which we are going to pass in selection criteria then in the $set we are going to update employee Profession from “Developer” to “C# Developer”.

Note

Options are Optional.

MongoDB
MongoDB

Using updateMany Method (To update only single document)

Updates multiple documents within the collection based on the filter.

For updating document we first need to pass the selection criteria for the update (means a unique id or field on which we can select fields to update) then after that, we need to pass data to update (document).

In this part we are going to update employee details based on Profession which we are going to pass in selection criteria then in the $set we are going to update employee Salary to “60000”.

MongoDB
MongoDB

Using Count () Method

Count Number of Document in Collection

Using Collection.count() method we can count a number of the document in particular collection.

In this part, we are going to count a number of employees in collections.

MongoDB
MongoDB

Using count () Method with query selection criteria

Using count () method with selection criteria we can count a number of the document in particular collection which is returned according to criteria.

In this part, we are going to count a number of employees whose Name equals “Sai” (criteria) in collections.

MongoDB

Using Distinct Method

Finds the distinct values for a specified field across a single collection and returns the results in an array.

In this part, we are going use distinct method for getting distinct employees name from collections.

MongoDB

Reference from

https://docs.mongodb.com

In this article, we have learned how to set up MongoDB server and how to use basics Commands of MongoDB.


Similar Articles