如何设置useMongoClient(Mongoose 4.11.0)?

这是我用来连接到我的数据库的代码:

private connectDatabase(databaseUri: string): Promise<Mongoose.Connection> { return Mongoose.connect(databaseUri).then(() => { debug('Connected to MongoDB at %O', databaseUri); return Mongoose.connection; }); } 

今天我更新了Mongoose到版本4.11.0,并且在运行我的testing时遇到了这个警告:

 (node:4138) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()` 

我找不到任何有关如何设置useMongoClient的信息。

你们知道如何?

这是你如何使用useMongoClientmongoose.connect mongoose.connect('mongodb://localhost/advisorDemoTestDB', { useMongoClient: true })

添加{useMongoClient:true}作为connect或createConnection方法的另一个参数,它依赖于你正在使用的mongoose的版本。

 // Using `mongoose.connect`... var promise = mongoose.connect('mongodb://localhost/myapp', { useMongoClient: true, /* other options */ }); // Or `createConnection` var promise = mongoose.createConnection('mongodb://localhost/myapp', { useMongoClient: true, /* other options */ }); 
 mongoose.connection.openUri('mongodb://127.0.0.1/camp_v12') 

有没有人试过这个? 当我使用这个时,我的弃用警告消失了,它来自文档

http://mongoosejs.com/docs/connections.html

最简单的解决办法是打开你的terminal,把你的目录改成你的根项目( package.json所在的文件夹)

跑:
npm remove mongoose

然后:

npm install mongoose@4.10.8 --save

问题解决了。

升级并不总是最好的select。

http://mongoosejs.com/docs/connections.html

使用Mongoose 4.11.x连接到MongoDB(使用mLab单实例和MongoDB Atlas副本集进行testing):

 const mongoose = require('mongoose');

 mongoose.Promise = global.Promise;

 const options = {
   promiseLibrary:global.Promise,
   useMongoClient:true,
 };

函数connect(){
   mongoose.connect(URI,选项)
     .then(function(){
       const admin = new mongoose.mongo.Admin(mongoose.connection.db);
       admin.buildInfo(function(err,info){
        如果(err){
           console.err(错误获取MongoDB信息:$ {err}`);
         } else {
           console.log(`连接到MongoDB(版本$ {info.version})打开成功!`);
         }
       });
     })
     .catch((err)=> console.error(错误连接到MongoDB:$ {err}`));
 }

 module.exports = connect;

创build模型:

 const mongoose = require('mongoose');

 const userSchema = new mongoose.Schema({...});

 module.exports = mongoose.model('User',userSchema);

没有Typescript,你几乎可以忽略这个问题,并使用Mongoose.connect(databaseUri, { useMongoClient: true })

如果你真的想避免警告避免版本4.11.0。

我更新到版本4.11.1,因为@ types / mongoose @ 4.7.18还没有更新,他们没有提到在ConnectionOptions的字段useMongoClient ,这是我如何导入模块:

 const Mongoose = require('mongoose'); 

然后使用这个function:

 private connectDatabase(databaseUri: string): Promise<any> { return Mongoose.connect(databaseUri, { useMongoClient: true }) .then(() => { console.log('Connected to MongoDB at ', databaseUri); return Mongoose.connection; }) .catch(err => debug(`Database connection error: ${err.message}`)); } 

根据mongoose 文档 ,这是如何设置useMongoClient

 function connectDatabase(databaseUri){ var promise = mongoose.connect('mongodb://localhost/myapp', { useMongoClient: true, }); return promise; } 

最简单的解决办法是 "npm remove mongoose"然后"npm install mongoose@4.10.8 --save"问题解决了。 升级并不总是最好的select。

 var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/starbucks', { useMongoClient: true }); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('openUri', function() { // we're connected! }); 

最简单的解决方法是:

通过terminal在项目根文件夹中运行此命令:

npm remove mongoose

npm install mongoose@4.10.8 --save

问题解决了。

升级并不总是最好的select。

https://github.com/Automattic/mongoose/issues/5399

错误:

(node:1652)DeprecationWarning: open()在mongoose> = 4.11.0中被弃用,使用openUri() ,或者在使用connect()createConnection()设置useMongoClient选项。

解决scheme:在连接属性中设置useMongoClient:true

 var promise = mongoose.connect('mongodb://localhost/myapp', { useMongoClient: true, /* other options */ }); 

请参阅http://mongoosejs.com/docs/connections.html#use-mongo-client现在正在侦听请&#x6C42;

它适用于我使用Typescript:

 var dbOpt : any = { useMongoClient: true } mongoose.connect(dbURI, dbOpt);