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?
-
1What 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 thatMatthew Verstraete– Matthew Verstraete2016-03-02 21:08:12 +00:00Commented Mar 2, 2016 at 21:08
-
1Thanks @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 ?vkt– vkt2016-03-02 21:10:02 +00:00Commented Mar 2, 2016 at 21:10
-
1My 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 ).vkt– vkt2016-03-02 21:38:08 +00:00Commented Mar 2, 2016 at 21:38
-
1I 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 helpMatthew Verstraete– Matthew Verstraete2016-03-02 21:53:23 +00:00Commented Mar 2, 2016 at 21:53
-
1Thanks 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.vkt– vkt2016-03-02 22:13:11 +00:00Commented Mar 2, 2016 at 22:13
3 Answers
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();
});
1 Comment
MongoClient.connect is deprecatedIt 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
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