Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to create new Mongodb database using Node.js ?

  • Last Updated : 20 Nov, 2020

mongodb module: This Module is used to performing CRUD(Create Read Update Read) Operations in MongoDb using Node.js. We cannot make a database only. We have to make a new Collection to see the database. The connect() method is used for connecting the MongoDb server with the Node.js project.

Please refer this link for connect() method.

Installing module:

npm install mongodb

Starting MongoDB server:

mongod --dbpath=data --bind_ip 127.0.0.1
  1. Data is the directory name where server is located. 
  2. 127.0.0.1 ip address where server will be running.



Project Structure:

Filename index.js

Javascript




const MongoClient = require('mongodb');
  
// server location
MongoClient.connect(url).then((client) => {
  
    console.log('Database created');
      
    // database name
    const db = client.db("GFGNodejs");
      
    // collection name
    db.createCollection("GFGNEW");
})

Output:

Mongodb Database:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!