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
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
Installation of MongoDB server
Installing 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 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.
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]
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.
And all your data files of MongoDB will be stored in [C:\data\db].
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.
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].
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.
After entering --dbpath path, just press Enter.
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].
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.
Source
https://www.mongodb.com
After connecting to MongoDB, we are going to learn how to create a database of MongoDB from the command prompt (CLI).
Creating MongoDB Database.
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.
Now, we have created demodatabase Database.
See List Databases
If we want to see created Database, then we need to enter
show dbs
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
Next step, we are going to create collection in Database with name “employee”.
Now, we have created Collection. Let’s run command “show dbs” to see a list of the databases we have by now.
Wow, we have 2 databases and we can see database which we have created.
Drop Database in MongoDB
To drop database which we have created, just type command
db.dropDatabase()
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
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
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
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.
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.
Using remove Method (To Delete Single Document)
Delete a single document or all documents that match a specified filter.
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.
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.
Deleting all Document
- 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.
We just need to pass Collection name and then remove method with empty document ({ }).
- 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.
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
- The $set operator replaces the value of a field with the specified value.
- To update multiple documents set the Multi parameter to true by default it is false in options
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.
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”.
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.
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.
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.
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.