为什么我收到这个弃用的警告?! MongoDB的
我在NodeJS中使用MongoDB,
const { MongoClient, ObjectId } = require("mongodb");
const MONGO_URI = `mongodb://xxx:xxx@xxx/?authSource=xxx`; // prettier-ignore
class MongoLib {
constructor() {
this.client = new MongoClient(MONGO_URI, {
useNewUrlParser: true,
});
this.dbName = DB_NAME;
}
connect() {
return new Promise((resolve, reject) => {
this.client.connect(error => {
if (error) {
reject(error);
}
resolve(this.client.db(this.dbName));
});
});
}
async getUser(collection, username) {
return this.connect().then(db => {
return db
.collection(collection)
.find({ username })
.toArray();
});
}
}
let c = new MongoLib();
c.getUser("users", "pepito").then(result => console.log(result));
c.getUser("users", "pepito").then(result => console.log(result));
当执行最后一个c.getUser语句时(也就是说,当我进行SECOND连接时),Mongodb输出此警告:
the options [servers] is not supported
the options [caseTranslate] is not supported
the options [username] is not supported
the server/replset/mongos/db 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,keepAliveInitialDelay,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,checkServerIdentity,validateOptions,appname,auth,user,password,authMechanism,compression,fsync,readPreferenceTags,numberOfRetries,auto_reconnect,minSize,monitorCommands,retryWrites,useNewUrlParser]
但我没有使用任何弃用的选项。有任何想法吗?
编辑
在评论中与molank进行了一些讨论后,看起来打开来自同一服务器的几个连接并不是一个好习惯,所以也许这就是警告试图说的(我认为很糟糕)。因此,如果您遇到同样的问题,请保存连接而不是mongo客户端。
回答如下:从https://jira.mongodb/browse/NODE-1868转发:
弃用消息很可能是因为client.connect
被多次调用。总的来说,当前多次调用client.connect
(从驱动程序v3.1.13
开始)具有未定义的行为,并且不建议这样做。重要的是要注意,一旦从connect
返回的承诺结算,客户端将保持连接,直到您调用client.close
:
const client = new MongoClient(...);
client.connect().then(() => {
// client is now connected.
return client.db('foo').collection('bar').insertOne({
}).then(() => {
// client is still connected.
return client.close();
}).then(() => {
// client is no longer connected. attempting to use it will result in undefined behavior.
});
默认情况下,客户端与其连接的每个服务器保持多个连接,并可用于多个同时操作*。您应该运行client.connect
一次,然后在客户端对象上运行您的操作
*请注意,客户端不是线程安全的或叉安全的,因此不能跨叉共享,并且它与节点的cluster
或worker_threads
模块不兼容。
为什么我收到这个弃用的警告?! MongoDB的
我在NodeJS中使用MongoDB,
const { MongoClient, ObjectId } = require("mongodb");
const MONGO_URI = `mongodb://xxx:xxx@xxx/?authSource=xxx`; // prettier-ignore
class MongoLib {
constructor() {
this.client = new MongoClient(MONGO_URI, {
useNewUrlParser: true,
});
this.dbName = DB_NAME;
}
connect() {
return new Promise((resolve, reject) => {
this.client.connect(error => {
if (error) {
reject(error);
}
resolve(this.client.db(this.dbName));
});
});
}
async getUser(collection, username) {
return this.connect().then(db => {
return db
.collection(collection)
.find({ username })
.toArray();
});
}
}
let c = new MongoLib();
c.getUser("users", "pepito").then(result => console.log(result));
c.getUser("users", "pepito").then(result => console.log(result));
当执行最后一个c.getUser语句时(也就是说,当我进行SECOND连接时),Mongodb输出此警告:
the options [servers] is not supported
the options [caseTranslate] is not supported
the options [username] is not supported
the server/replset/mongos/db 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,keepAliveInitialDelay,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,checkServerIdentity,validateOptions,appname,auth,user,password,authMechanism,compression,fsync,readPreferenceTags,numberOfRetries,auto_reconnect,minSize,monitorCommands,retryWrites,useNewUrlParser]
但我没有使用任何弃用的选项。有任何想法吗?
编辑
在评论中与molank进行了一些讨论后,看起来打开来自同一服务器的几个连接并不是一个好习惯,所以也许这就是警告试图说的(我认为很糟糕)。因此,如果您遇到同样的问题,请保存连接而不是mongo客户端。
回答如下:从https://jira.mongodb/browse/NODE-1868转发:
弃用消息很可能是因为client.connect
被多次调用。总的来说,当前多次调用client.connect
(从驱动程序v3.1.13
开始)具有未定义的行为,并且不建议这样做。重要的是要注意,一旦从connect
返回的承诺结算,客户端将保持连接,直到您调用client.close
:
const client = new MongoClient(...);
client.connect().then(() => {
// client is now connected.
return client.db('foo').collection('bar').insertOne({
}).then(() => {
// client is still connected.
return client.close();
}).then(() => {
// client is no longer connected. attempting to use it will result in undefined behavior.
});
默认情况下,客户端与其连接的每个服务器保持多个连接,并可用于多个同时操作*。您应该运行client.connect
一次,然后在客户端对象上运行您的操作
*请注意,客户端不是线程安全的或叉安全的,因此不能跨叉共享,并且它与节点的cluster
或worker_threads
模块不兼容。