In this era of mass development developers needed a database system where they can quickly transfer data between database and server. Previously we had SQL (Structured Query Language) database management system which put more importance on storage uses and security. SQL database systems are strict when making queries. Whereas NoSQL database management system focuses on easier and faster data transfer to keep up with the massively growing data demand from the client-side. MongoDB is a NoSQL or non-relational database management system which is one of the best database management systems right now. Today we’ll talk about it and learn how to make the basic setup to use it on our website.
NoSQL database is a document-based database management system. MongoDB takes data in JSON format and stores it in the database. It is best known for its effective and fast data transfer speed. But for a beginner, it can get quite complicated to create the basic structure to use MongoDB in their website.
Create account
To start using it we first need to create an account on the MongoDB website. After we’ve successfully created it we can move on to the next stage.
Create new user
Now we need to create a new user to use the database. We can go to Database Access and click on Add new Database User button –
We need to save the username and password to use it later to connect with the database.
Network access
We can give a specific IP address to give access to the database if we want or we can give 0.0.0.0 as an IP address to make it accessible in all IP addresses –
Create the basic structure
To connect our database with our server we need to create a connection URI where we need to use our username and password which we previously saved. To do this we need to go to the Database page and click on connect –
Then we can click on the “Connect your application” button –
Connect database with server-side
After we’ve clicked “Connect your application” we’ll be given a code that we can directly copy-paste in our server-side code file to begin the connection process –
const { MongoClient } = require("mongodb");
const uri =
"mongodb+srv://<username>:<password>@cluster0.801m1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
client.connect((err) => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
- Here in the first line, we are importing MongoDB to use it. Before we do that we need to install MongoDB on our server-side using the npm command line “npm install mongodb”.
- Then we created the connection uri. Here we need to give the username and password replacing and . Then we are creating a new client using the uri.
- After that, we are connecting the client with the database. Here we are also creating a database named “test” and a collection named “devices”. But there are better ways to do this. Following these steps, we have successfully created a database and connected it with our server-side. Now we can write API to transfer data between server and database.
Create database and collection manually
We can also create a database and a collection manually. To do this we need to click on Database and click on “Browse Collections” –
We’ll be navigated to the Collection page. Then we need to click on "Create Database" button.
Then we need to write a database name and collection name and click on create. This will create a new database and collection.
Add data manually in the database
We can manually add data into the database by inserting JSON type data in the box. Click on the “Insert Document” button –
Here we can write or paste the JSON data to store in the database –
Then click on insert to finish storing it.
So, following these simple steps we can create a new database in MongoDB, establish a connection of server-side with database, create database, and collection and store data in the new collection.
Conclusion
MongoDB has made it possible to developers to store different types of data in an unstructured format which enables faster data transfer. But for a beginner, it may be a little complex at the start but it’ll be a lot easier once they complete it once. I hope this simple guide gives the new MongoDB developer an understanding of what MongoDB is and how they can use it in their project.
Top comments (1)
Was looking for that! Nice :)