In this part of the mongodb operations using node.js tutorial series , we will create a collection
details
in MongoDb
using Node.js
We will start by including mongodb
npm package as shown below :
var mongo = require('mongodb');
Establish a connection between the mongoDb database and our node.js app using the following :
var new_db = "mongodb://localhost:27017/demo_db"
createCollection()
is an inbuilt method which is used to create a new collection in mongodb database
as shown below:
//create-collection.js
var mongo = require('mongodb');
var new_db = "mongodb://localhost:27017/demo_db";
mongo.connect(new_db , (error , db) => {
if (error){
throw error;
}
//CREATING A COLLECTION IN MONGODB USING NODE.JS
db.createCollection("details" , (err , collection) => {
if(err) throw err;
console.log("Details collection created successfully");
});
});
>node create-collection.js
Details collection created successfully
In this article we learned about
monogdb
npm package in your app.