0
我想用nodejs连接到mongodb。我想连接到一次MongoDB并使用需要的实例。使用nodejs的MongoClient连接
connection.js文件
var MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server;
var mongoClient = new MongoClient(new Server('localhost', 27017));
module.export = mongoClient;
我要访问其他文件MongoClient对象执行类似操作分贝:
router.js文件
var mongoClient = require('./connection.js')
mongoClient.open(function(err, mongoClient) {
var db1 = mongoClient.db("mydb");
db1.collection('Persons', function (err, collection) {
collection.find().toArray(function(err, items) {
if(err) throw err;
console.log(items);
});
mongoClient.close();
});
我我得到以下错误:
TypeError: mongoClient.open is not a function