简体   繁体   English

如何使用Node JS驱动程序创建新的MongoDb数据库

[英]How to create New MongoDb Database using the Node JS driver

I want to create a new Database in MongoDB using the Node JS driver. 我想使用Node JS驱动程序在MongoDB中创建一个新数据库。 I tried the following approaches, but none of them created any databases (I checked using the mongo shell and RoboMongo) and the worst part is, it is not showing any errors, below programs are executed successfully without any error (I mean, error is NULL) 我尝试了以下方法,但没有一个方法创建任何数据库(我使用mongo shell和RoboMongo检查了),最糟糕的是,它没有显示任何错误,下面的程序已成功执行,没有任何错误(我的意思是,错误是空值)

  • First Method, using the Mongo Server 第一种方法,使用Mongo Server

 var Db = require('mongodb').Db, Server = require('mongodb').Server; var db = new Db('myNewDatabase', new Server('localhost', 27017)); db.open(function (err, db) { if (err) { console.dir('ERROR we are in the callback of the open '); console.dir(err); throw err; } // Use the admin database for the operation var adminDb = db.admin(); console.dir('we are in the callback of the open'); db.close(); }); 

  • Second approach I followed is: 我遵循的第二种方法是:

 var server = "localhost"; var port = 27017; var dbName = "myNewDatabase"; var mongodb = require('mongodb'); var mongoClient = mongodb.MongoClient; var connString = "mongodb://"+server+":"+port+"/"+dbName; mongoClient.connect(connString, function(err, db) { console.dir(err); if(!err) { console.log("\\nMongo DB connected\\n"); db.collection('test_correctly_access_collections', function(err, col2) { console.dir(err); if(err) { console.dir('Thier is a error in creating collection'); console.dir(err); } console.log("\\nColllection created succesfully\\n"); db.close(); }); } else{ console.log("Mongo DB could not be connected"); process.exit(0); } }); 

According to this link , we can use getDatabase API to create a new database, i tried for the same API in the Node JS, but i am unable to find one. 根据此链接 ,我们可以使用getDatabase API创建一个新的数据库,我尝试在Node JS中使用相同的API,但是找不到。

As searched in google and stackOverflow for this question, but I am able to find, only very few. 正如在google和stackOverflow中搜索的这个问题,但我只能找到很少的问题。 So i am posting the answer to this myself. 因此,我自己发布了答案。

At first thank you @somallg , you are right i voted up your comment. 首先感谢您@somallg,您是对的,我对您的评论投了赞成票。

Answer is, you need to insert document into collection and then, MongoDB will create the New Database and also the collection. 答案是,您需要将文档插入集合中,然后,MongoDB将创建新数据库以及集合。 So, above approaches in my question can we re-written as follows to create a new database using the Node JS Driver: 因此,我们可以将上述问题中的上述方法重写如下,以使用Node JS Driver创建新数据库:

  • First Appoarch 初审

 var Db = require('mongodb').Db, Server = require('mongodb').Server; var db = new Db('myNewDatabase', new Server('localhost', 27017)); db.open(function (err, db) { if (err) { console.dir('ERROR we are in the callback of the open '); console.dir(err); throw err; } var collection = db.collection("simple_document_insert_collection_no_safe"); collection.insert({hello:'world_no_safe'}); console.dir('we are in the callback of the open'); db.close(); }); 

  • second approach 第二种方法

 var server = "localhost"; var port = 27017; var dbName = "myNewDatabase"; var mongodb = require('mongodb'); var mongoClient = mongodb.MongoClient; var connString = "mongodb://"+server+":"+port+"/"+dbName; mongoClient.connect(connString, function(err, db) { console.dir(err); if(!err) { var collection = db.collection("simple_document_insert_collection_no_safe"); collection.insert({hello:'world_no_safe'}); } else{ console.log("Mongo DB could not be connected"); process.exit(0); } db.close(); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM