Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我会收到这个不推荐的警告?!蒙哥达_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript 为什么我会收到这个不推荐的警告?!蒙哥达

Javascript 为什么我会收到这个不推荐的警告?!蒙哥达,javascript,node.js,mongodb,Javascript,Node.js,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, { useN

我在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语句时(也就是说,当我进行第二次连接时),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客户端。

函数
.connect()
采用3个参数,并定义为
MongoClient.connect(url[,options],callback)
。因此,您需要首先提供一个URL,然后是选项,然后才给它回调。下面是文档中的一个示例

MongoClient.connect("mongodb://localhost:27017/integration_tests", { native_parser: true }, function (err, db) {
    assert.equal(null, err);

    db.collection('mongoclient_test').update({ a: 1 }, { b: 1 }, { upsert: true }, function (err, result) {
        assert.equal(null, err);
        assert.equal(1, result);

        db.close();
    });
});
另一种方法是,因为您已经创建了
MongoClient
,所以使用
。打开
。它只接受一个回调,但是您可以从您创建的
(this.client)
mongoClient调用它。你可以这样使用它

this.client.open(function(err, mongoclient) {
    // Do stuff
});
注 请务必查看,您会发现许多很好的示例,这些示例可能会更好地指导您。

从以下位置重新发布:

弃用消息可能是因为正在多次调用
client.connect
。总的来说,当前多次调用
client.connect
(从驱动程序
v3.1.13
开始)具有未定义的行为,不建议这样做。请务必注意,一旦从
connect
返回的承诺得到解决,客户端将保持连接状态,直到您调用
client。关闭

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.
});
默认情况下,客户机与所连接的每台服务器保持多个连接,并可用于多个同时操作*。您应该可以正常运行
客户端。连接
一次,然后在客户端对象上运行您的操作


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

这并不能回答我的问题。open()也不推荐使用。正如文档所说,我正在连接,但是当我建立出现的第二个连接时。谁说
open
不推荐?因为文档中没有提到这一点。另外,您在问题中没有说您连接了两次我的错,open()在mongoose中不受欢迎,但我没有使用它。抱歉,混淆了。是的,我忘了说发生在我第二次连接时,我要去修复它。很抱歉,我没有明确地告诉你,我现在不知道了,嗯,是的,但是你可以创建一个新的MongoClient实例(uri,选项)(我们称之为db),然后调用db.connect(回调)。文件:。实际上,如果我尝试使用open(),它会输出一个错误,告诉我open()不是一个函数