13

I am able to drop database using node.js but I dont know how to create new database? I want to create monogo database using node.js. Can someone help me about how to create mongo database using node.js?

5
  • 1
    What have you tried so far? Have you checked out google yet. if your running into a specific problem show your code and the problem and we can help you deal with that Commented Mar 2, 2016 at 21:08
  • 1
    Thanks @MatthewVerstraete, Yes I checked it but I am not getting any useful answers. db.dropDatabase('test'); using this I am deleting my database. Now I want to create the same "test" database using code into my node.js file. Can you help me for the solution please ? Commented Mar 2, 2016 at 21:10
  • 1
    My question is, In mongodb I can create database using "use dbname". Is is possible to create database using node.js in mongodb ( not using mongodb interface ). Commented Mar 2, 2016 at 21:38
  • 1
    I don't use either of these tech but a simple google search is showing me a lot of response on how to create a mongodb database in node.js. If these are not helping you then I can't be of much more help. Edit your post with your code, a link to the tutorial/doc your using, and the error your getting and maybe others can help Commented Mar 2, 2016 at 21:53
  • 1
    Thanks Mathhew, I got a solution. We do not have any query for creating database using node.js but If you code for insertion then it would automatically create database for you. Commented Mar 2, 2016 at 22:13

3 Answers 3

11

you can do it this way:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydatabase"; // mydatabase is the name of db 
MongoClient.connect(url, function(err, db) {
  if (err) throw err;
console.log("Database created!");
  db.close();
});
Sign up to request clarification or add additional context in comments.

1 Comment

MongoClient.connect is deprecated
8

It depends on what you're using to interact with mongo, but you don't usually need to create the database. It's usually just created when you use it in your code.

For example if you use the mongodb native driver:

MongoClient.connect('mongodb://localhost:27017/myNewDatabase', function(err, db) {
  // If a database called "myNewDatabase" exists, it is used, otherwise it gets created.

  var collection = db.collection('myNewCollection');
  // If a collection called "myNewCollection" exists, it is used, otherwise it gets created.
});

The driver api docs have lots of examples - http://mongodb.github.io/node-mongodb-native/2.0/api/

Comments

7

At first, install MongoDB module by running this command.

npm install mongodb --save

Just keep in mind that you can not create only database. You have to create also the collection to see your new database.

index.js

const mongodb = require('mongodb');
const uri = 'mongodb://localhost:27017';
const client = new mongodb.MongoClient(uri);

client.connect((err) => {
    if (!err) {
        console.log('connection created');
    }
    const newDB = client.db("YourNewDatabase");
    newDB.createCollection("YourCreatedCollectionName"); // This line i s important. Unless you create collection you can not see your database in mongodb .

})

Now run this index file by the following command in your terminal

node index.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.