AlexPham
AlexPham

Reputation: 321

Create new mongoDB with nodejs

I installed MongoDB 4.0 in local host. There are 3 default database as below

> show dbs
admin   0.078GB
config  0.078GB
local   0.078GB

Then I tried to create a new db using nodejs

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://local:27017/mydatabase"; // mydatabase is the name of db 
MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    console.log("Database created!");
    db.close();
});

Above code work fine. I can see message "Database created!".

However , when I show dbs again , newly created mydatabase does not show up. There are still 3 databases .

> show dbs
admin   0.078GB
config  0.078GB
local   0.078GB

May I know what I did wrong ?

Thanks ALex

Upvotes: 1

Views: 561

Answers (2)

Bharathvaj Ganesan
Bharathvaj Ganesan

Reputation: 3194

Important thing to know while creating MongoDB database is that A database is not created until it gets content! . You need to create at least one collection.

Hopes this clears your doubt.

Upvotes: 3

Yogesh.Kathayat
Yogesh.Kathayat

Reputation: 1004

Your MongoDb database will not be created until you create at least one collection with at least one record, so create a collection and add at least one record in it to see the database using 'show dbs'.

Upvotes: 1

Related Questions