代码之家  ›  专栏  ›  技术社区  ›  Antonio Gamiz Delgado

为什么我收到这个被否决的警告?蒙哥大

  •  0
  • Antonio Gamiz Delgado  · 技术社区  · 6 年前

    我在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]
    

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


    编辑

    经过一番讨论 摩洛克人 在注释中,从同一服务器上打开多个连接似乎不是一个好的实践,所以可能警告就是这么说的(我认为很糟糕)。因此,如果您有相同的问题,请保存连接,而不是Mongo客户机。

    1 回复  |  直到 6 年前
        1
  •  1
  •   molamk    6 年前

    函数 .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 是使用 .open 相反。它 只接受回拨 但是你可以从 mongoClient 你创造 (this.client) . 你可以这样用

    this.client.open(function(err, mongoclient) {
        // Do stuff
    });
    

    注释

    确保您查看 MongoClient docs 你会发现很多很好的例子,可以更好地指导你。