Node Js - Setting Up Mongo DB database for your application
Steps:
1. Download MongoDB community server
Once the download is complete, Launch the msi file
Then, Accept the End-user license agreement and click next
Select the option "Install MongoD as a service" and click next
Mongo Db installer gives you the option to install the MongoDB compass which is an official GUI tool for MongoDB. Check the checkbox "Install MongoDB compass" and click next.
If you have any other GUI tool on your machine, do not check the MongoDB compass option.
Finally, click Install
The complete installation should take a few minutes to complete, after that click finish.
There are 2 ways you can run MongoDB:
1. Run MongoDB as a windows service
2. Run MongoDB from the windows command prompt.
Create a database directory in your system, where MongoDB stores data.
In your C drive (or your root directory), create the data directories from the cmd:
md "\data\db"
This should be your directory absolute path : C:\data\db
2. Start MongoDB database
Run the following command in the cmd
"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath="c:\data\db"
--dbpath points to your database directory.
Note: we are using the complete path to mongod.exe but you can also set the path till bin directory in the environment variable and simply use below in the command,
mongod --dbpath ="c:\data\db"
If MongoDB server started successfully, you should get the messages like "Listening on 127.0.0.1" and "Waiting for connections on port 27017".
3. Connect to MongoDB
If you have set the environment path for mongo.exe, then simply to connect to MongoDB server.
Run the command in cmd: mongo
or With the complete path (If environment path is not set)
Run in cmd: "C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe"
Now, You can successfully connect with your MongoDB server.
Let's learn about MongoDB Compass which is an official GUI tool for MongoDB to explore and manipulate your MongoDB data.
Mongo Db installer gives you the option to install the MongoDB compass which is an official GUI tool for MongoDB. Check the checkbox "Install MongoDB compass" and click next.
If you have any other GUI tool on your machine, do not check the MongoDB compass option.
Finally, click Install
The complete installation should take a few minutes to complete, after that click finish.
As we have installed MongoDB as a windows service. The MongoDB service is started upon successful installation, But If you only installed the executables and did not install MongoDB as a windows service, you have to manually start the MongoDB instance.
There are 2 ways you can run MongoDB:
1. Run MongoDB as a windows service
2. Run MongoDB from the windows command prompt.
Run MongoDB as a windows service
- During the installation of MongoDB, we have installed MongoDB as a windows service, that started running after the successful installation itself. Also, you can manually start the service from the services console.
- To begin using MongoDB, connect a mongo.exe shell to the running MongoDB instance. Open cmd with admin rights and run:
"C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe" - You can stop or pause the running windows service from the services console.
Run MongoDB from the command prompt
You can run MongoDB from the Windows command prompt instead of as a service.1. Create a database directory
Create a database directory in your system, where MongoDB stores data.
In your C drive (or your root directory), create the data directories from the cmd:
md "\data\db"
This should be your directory absolute path : C:\data\db
2. Start MongoDB database
Run the following command in the cmd
"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath="c:\data\db"
--dbpath points to your database directory.
Note: we are using the complete path to mongod.exe but you can also set the path till bin directory in the environment variable and simply use below in the command,
mongod --dbpath ="c:\data\db"
If MongoDB server started successfully, you should get the messages like "Listening on 127.0.0.1" and "Waiting for connections on port 27017".
3. Connect to MongoDB
If you have set the environment path for mongo.exe, then simply to connect to MongoDB server.
Run the command in cmd: mongo
or With the complete path (If environment path is not set)
Run in cmd: "C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe"
Now, You can successfully connect with your MongoDB server.
Let's learn about MongoDB Compass which is an official GUI tool for MongoDB to explore and manipulate your MongoDB data.
Download the MongoDB Compass GUI tool
We have already downloaded the MongoDB compass with the installation of the MongoDB community server.
Anyway, If you have skipped the MongoDB Compass installation earlier, you can download it from here: https://docs.mongodb.com/compass/current/install/.
Open the Compass GUI
Connect to the Database
There are 2 ways you can connect to the DB
1. Fill connection fields individually
2. Directly provide your connection string and connect to the DB
Fill in Connection fields individually
Click Connect and we are now connected to our DB.
0 Comments