为什么我会收到此已过时的警告?! MongoDB的 [英] Why am I getting this deprecated warning?! MongoDB

查看:488
本文介绍了为什么我会收到此已过时的警告?! MongoDB的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在NodeJS中使用MongoDB,

I'm working with MongoDB in NodeJS,

    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语句时(也就是说,当我进行第二次连接时),Mongodb输出以下警告:

and when the last c.getUser statement is executed (that's to say, when I make a SECOND connectio) Mongodb outputs this warning:

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]

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

But I'm not using any deprecated options. Any ideas?

编辑

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

After a little discussion with molank in the comments, it looks like open several connections from the same server is not a good practice, so maybe that's what the warning is trying to say (badly I think). So if you have the same problem, save the connection instead of the mongo client.

推荐答案

https://重新发布jira.mongodb.org/browse/NODE-1868 :

弃用消息很可能是因为多次调用了client.connect.总体而言,当前多次调用client.connect(从驱动程序v3.1.13开始)具有未定义的行为,因此不建议这样做.重要的是要注意,一旦从connect返回的承诺解决,客户端将保持连接状态,直到您呼叫client.close:

The deprecation messages are likely because client.connect is being called multiple times. Overall, calling client.connect multiple times currently (as of driver v3.1.13) has undefined behavior, and it is not recommended. It is important to note that once the promise returned from connect resolves, the client remains connected until you call 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,然后在客户端对象上运行操作

The client by default maintains multiple connections to each server it is connected to, and can be used for multiple simultaneous operations*. You should be fine running client.connect once, and then running your operations on the client object

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

* Note that the client is NOT thread-safe or fork-safe, so it cannot be shared across forks, and it not compatible with node's cluster or worker_threads modules.

这篇关于为什么我会收到此已过时的警告?! MongoDB的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆