【问题标题】:Getting deprecated warnings on Mongodb in node.js在 node.js 中获取有关 Mongodb 的已弃用警告
【发布时间】:2019-11-08 23:34:30
【问题描述】:

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

【问题讨论】:

    标签: javascript node.js mongodb


    【解决方案1】:

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

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

    我看到了另一个 SO 问题:

    The options [useMongoClient] is not supported

    这也可能有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2021-07-12
      • 2023-04-07
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 2020-12-06
      • 2015-03-23
      相关资源
      最近更新 更多