In this Chapter , we will create a collection name "details" within our database name "demo_db" in the NoSql , Document oriented database MongoDb using the Server Side platform Node.js
Step - 1 : Including Packages
We will start by
requiring the require package.
We are using the following package in our application :
var mongo = require('mongodb');
Step - 2 : Establish Connection
Now let's establish a connection between the mongoDb Database and our node.js application.
var new_db = "mongodb://localhost:27017/demo_db"
Step - 3 : Create the collection
createCollection() is an inbuilt method used to Creates a new collection or view.
//create_collection_mongo_nodejs.js
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");
});
});
D:\nj-learn-mongo>node create_collection_mongo_nodejs.js
D:\nj-learn-mongo>node create_collection_mongo_nodejs.js
Details collection created successfully