DEV Community

Cover image for Set up MongoDB with node.js :
Rahul Kumar
Rahul Kumar

Posted on

Set up MongoDB with node.js :

What is MongoDB :

It is a general purpose document based NoSQL database . Build for scalable application .

What is Mongoose :

  • mongoose is a object document mapper (ODM) used for storing and managing data in form of objects .
  • Which makes our code more simpler to write than the MongoDB native driver .

What is mongod:

Mongod is the demon for MongoDB which performs background management operations and actively listening for the incoming request on port 2701.

What is mongo shell :

This is the command line utility for performing CRUD operations on command line .

What is NodeJS :

Node.js is a backend framework for building backend web application .

npm i mongoose

add this URL for the database connection

mongoose.connect("mongodb://localhost:27017/(name of your db)", {useNewUrlParser:true, useUnifiedTopology: true});

Create the schema of our db

const newSchema = mongoose.Schema({
   name : String,
   rating : Int
});
Enter fullscreen mode Exit fullscreen mode

Create a new mongoose model the singular version of our db .

const Post = new mongoose.model('Post', newSchema);
Enter fullscreen mode Exit fullscreen mode

Create a new Document :

const Post_a = new Post({
name : "Dev post",
rating : 7
});
Enter fullscreen mode Exit fullscreen mode

save the new value into the database

Post.save();
Enter fullscreen mode Exit fullscreen mode
  • If you want to upload data into the cloud then you could upload to MongoDB atlas which is a database as a service .

Thank You For Reading 🤖🤖🤖

Top comments (0)