Example created collection in mongoDB with nodejs

Creating a Collection

To create a collection in MongoDB, use the createCollection() method:

Example:

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://192.168.1.122:27017/";

MongoClient.connect(url, function(err, db) {

  if (err) throw err;

  var dbo = db.db("mydb");

  dbo.createCollection("customers", function(err, res) {

    if (err) throw err;

    console.log("Collection created!");

    db.close();

  });

});

 

Run:

 

C:\ Nodejs>node mongodbCreatedCollection.js

Collection created!