Hello, Everyone! Starting with MongoDb!
What is MongoDB:
- In simple terms its a database where in we can store data!
- By defination, its a No-Sql database that stores a data in flexible and document-oriented format.
- Data is stores as JSON-like documnents.
- It is Scalable means it good for handling very large datasets.
- Here we can see the how the data store in db:
So let's start with how can you connect with MongoDB:
Go to https://www.mongodb.com/products/platform/atlas-database
Create your free account. This will be central hub for managing clusters.
Make a free cluster here.
- Here yo've to select free one and then just add the cluster name and select the create button.
- After Creating the cluster you'll get username and password which is important for connection string. -Copy this username , password.
- Now, Go to Network Access & add IP address just like this & then confirm it.
- Now Go to Cluster and click on Connect , it will display this page.
- Select connect to your application.Click on next
- You'll see connection string which is very important for to connect database, now replace db-password with your password and copy it.
From Here actual setup will start:
- Write npm init -y (initialize node project)
- Install mongoose for connection.
- Mongoose is an ODM (Object Data Modeling) library for MongoDB in Node.js.
Mongoose helps by adding a layer of structure and useful features on top of MongoDB.
Now make db folder and inside that db folder make db.connect.js and add this code , which allows us to connect with db
Make dotenv file where in you can store the connection string!
(npm install dotenv)Mkae a model folder, Inside it just add file like note.models.js (you can use anything).
Here we are creating a dataModel where in we can add the datatypes of data.
- Take a json Data from Chatgpt and give the name to that file like Notes.json!
- Remember the fields in json file is same as models.
Now Create another file which index.js where in you've to declare seedData fucntion.
Make a similar file , only you've to change is data inside the seed function & instead of cars.json file you've to use the notes.json file only!
call the SeedData function.
Execute node index.js you'll get
So this is the basic proces of connecting and seeding data in MongoDb.
Top comments (0)