为什么我会收到此已过时的警告?!MongoDB

Ant*_*ado 0 javascript mongodb node.js

我正在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));
Run Code Online (Sandbox Code Playgroud)

当最后一个c.getUser语句执行时(也就是说,当我进行第二次连接时),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]
Run Code Online (Sandbox Code Playgroud)

但是我没有使用任何不推荐使用的选项。有任何想法吗?


编辑

在评论中与molank进行了一些讨论之后,看来打开来自同一服务器的多个连接不是一个好习惯,因此,也许这就是警告想要说的(我认为很糟糕)。因此,如果您遇到相同的问题,请保存连接而不是mongo客户端。

dar*_*rpa 6

https://jira.mongodb.org/browse/NODE-1868转发:

可能会因为client.connect多次调用而弃用消息。总体而言,client.connect当前多次调用(从driver开始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.
});
Run Code Online (Sandbox Code Playgroud)

默认情况下,客户端与它连接的每个服务器保持多个连接,并且可用于多个同时操作*。您应该运行client.connect一次就可以了,然后在客户端对象上运行您的操作

*请注意,客户端不是线程安全或派生安全的,因此不能在派生之间共享,并且它与节点clusterworker_threads模块不兼容。