繁体   English   中英

在 node.js 中的 Mongodb 上收到不推荐使用的警告

[英]Getting deprecated warnings on Mongodb in node.js

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

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

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

我看到了另一个 SO 问题:

不支持选项 [useMongoClient]

这也可能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM