THE WORLD'S LARGEST WEB DEVELOPER SITE

Node.js MongoDB Create Table


Creating a Table

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

Example

Create a table called "customers":

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  db.createCollection("customers", function(err, res) {
    if (err) throw err;
    console.log("Table created!");
    db.close();
  });
});
Run example »

Save the code above in a file called "demo_create_mongo_table.js" and run the file:

Run "demo_create_mongo_table.js"

C:\Users\Your Name>node demo_create_mongo_table.js

Which will give you this result:

Table created!