mongoose连接扔警告

我正在写一个连接到MongoDB的应用程序,而像连接到服务器像bellow得到这样的警告:

Db.prototype.authenticate方法在下一个主版本3.x中将不再可用,因为MongoDB 3.6将只允许对pipe理数据库中的用户进行authentication,并且不再允许套接字上的多个凭证。 请使用具有授权凭证的MongoClient.connect进行validation。

我的代码如下图

import mongoose from 'mongoose'; import config from './config'; mongoose.connect(config.db.uri); 

在config.js

 const config = { name: 'API', version: '0.0.1', env: process.env.NODE_ENV || 'development', port: process.env.PORT || 3000, base_url: process.env.BASE_URL || 'http://localhost:3000', db: { uri: 'mongodb://admin:harry123@127.0.0.1:27017/ai?authSource=admin', }, } export default config; 

我正在使用节点v8.0.0和mongoose4.10.5

这是一个无害的警告和已知的mongoose问题 。 有关详细信息,请参阅mongoose线程,但是对于mongoose4.10.5,没有已知的解决方法,它不应该影响您的应用程序的function。

警告是由于MongoDB驱动程序不赞成mongoose的默认连接逻辑使用的API。 从useMongoClient 4.11.1开始,你可以通过设置useMongoClient选项来select使用mongo客户端,例如

 mongoose.connect(config.db.uri, { useMongoClient: true, /* other options */ }) 

需要提醒的是,使用Mongo本地客户端可能会产生不必要的影响,因此请务必详尽地testing所有内容
在这里看到更多细节

 Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials. 

是的,这是一个无害的警告和已知的mongoose问题。 正如@ Phu Ngo所说

所以,你可以通过更新到最新版本的4.11.1以上的mongoose来解决这个问题。

升级版本后,如果您使用了连接选项,则可能会出现此警告。

 he server/replset/mongos options are deprecated, all their options are supported at the top level of the options object [poolSize,ssl,sslValidate,sslCA,sslCert,sslKey,sslPass,sslCRL,autoReconnect,noDelay,keepAlive,connectTimeoutMS,family,socketTimeoutMS,reconnectTries,reconnectInterval,ha,haInterval,replicaSet,secondaryAcceptableLatencyMS,acceptableLatencyMS,connectWithNoPrimary,authSource,w,wtimeout,j,forceServerObjectId,serializeFunctions,ignoreUndefined,raw,bufferMaxEntries,readPreference,pkFactory,promiseLibrary,readConcern,maxStalenessSeconds,loggerLevel,logger,promoteValues,promoteBuffers,promoteLongs,domainsEnabled,keepAliveInitialDelay,checkServerIdentity,validateOptions,appname,auth] 

根据错误信息;

 the server/replset/mongos options are deprecated, all their options are supported at the top level of the options object 

所以解决这个问题的方法就是将设置选项从服务器,replset,socketOptions,mongos和其他任何层次结构选项移动到对象的顶层。

解决scheme与例子