在node.js中的Mongodb上获取过时的警告

我在Node.js上使用Mongodb官方驱动程序,当我对数据库进行查询时,我收到很多警告,这些警告对我没有任何意义。

我读到这可能是在同一服务器上打开多个连接的问题,但是我看不到为什么收到警告,因为在代码中可以看到我在返回结果之前关闭了每个连接。

这是我与驱动程序建立的库:

const {MongoClient,ObjectId} = require("mongodb");
const {mongoURI,mongoDbName} = require("../config/");
const debug = require("debug")("app:mongodb");

class MongoLib {
  constructor(collection) {
    this.client = new MongoClient(mongoURI,{
      useNewUrlParser: true,useUnifiedTopology: true
    });
    this.dbName = mongoDbName;
    this.collection = collection;
  }
  connect() {
    return new Promise((resolve,reject) => {
      this.client.connect(error => {
        if (error) {
          reject(error);
        } else {
          debug("Connection to mongodb succesful");
          resolve(this.client.db(this.dbName));
        }
      });
    });
  }

  createone(data) {
    return this.connect().then(db =>
      db
        .collection(this.collection)
        .insertOne(data)
        .then(result =>
          this.readOne({
            _id: result.insertedId
          }).then(async readed => {
            await this.client
              .close()
              .then(() => debug("closed mongodb connection"));
            return readed;
          })
        )
    );
  }

  readAll(query = {}) {
    return this.connect()
      .then(db =>
        db
          .collection(this.collection)
          .find(query)
          .toArray()
      )
      .then(async data => {
        await this.client
          .close()
          .then(() => debug("closed mongodb connection"));
        return data;
      });
  }

  readOne(query = {}) {
    return this.connect()
      .then(db => db.collection(this.collection).findOne(query))
      .then(async data => {
        await this.client
          .close()
          .then(() => debug("closed mongodb connection"));
        return data;
      });
  }
  readById(objectId) {
    return this.readOne({_id: new ObjectId(objectId)});
  }

  updateone(query = {},data = {}) {
    return this.connect().then(db =>
      db
        .collection(this.collection)
        .updateone(query,{$set: data})
        .then(() =>
          this.readOne({
            ...data
          }).then(async data => {
            await this.client
              .close()
              .then(() => debug("closed mongodb connection"));
            return data;
          })
        )
    );
  }
  updateoneById(objectId,data = {}) {
    return this.updateone({_id: new ObjectId(objectId)},data);
  }

  removeone(query = {}) {
    return this.connect()
      .then(db => db.collection(this.collection).deleteone(query))
      .then(async result => {
        await this.client
          .close()
          .then(() => debug("closed mongodb connection"));
        return result;
      });
  }
  removeoneById(objectId) {
    return this.removeone({_id: new ObjectId(objectId)});
  }
}

我收到此警告:

the options [servers] is not supported
the options [caseTranslate] is not supported
the options [dbName] is not supported
the options [srvHost] is not supported
the options [credentials] is not supported
the options [username] is not supported
the options [source] is not supported
the options [mechanism] is not supported
the options [mechanismProperties] is not supported
doubledd 回答:在node.js中的Mongodb上获取过时的警告

我相信您的MongoClient不再需要这些选项。您可以在选项的某处设置这些选项。我会在代码库中搜索这些选项的文本条目并将其删除。

我试图在Node JS MongoDB文档中搜索答案,但他们没有具体提及。我知道在升级MongoDB二进制文件和Java驱动程序时遇到了类似情况。版本向上升级时,我必须在Java驱动程序中使用不同的选项/方法。

我看到了另一个SO问题:

The options [useMongoClient] is not supported

这也可能有帮助。

本文链接:https://www.f2er.com/3140787.html

大家都在问