【问题标题】:Why will updateOne not work on my schema?为什么 updateOne 不能在我的架构上工作?
【发布时间】:2021-03-10 23:36:33
【问题描述】:

这是我当前的代码:

const mongoose = require("mongoose");
const schema = require("../schemas/ecos");
module.exports = async(req, res) => {
  const db = await schema.findOne({
      RobloxID: req.query.roblox_id
  });
  if(db.IsPendingVerification === "true") {
    res.send({
      success: true
    })
    db.updateOne({
      IsNowVerified: "true"
    })
  }
}

目前它不会更新数据库,所以 IsNowVerified 是真的。为什么会这样?我没有收到任何错误。

我的生态档案:

const mongoose = require("mongoose");
const productSchema = mongoose.Schema({
  Guild: { type: String, default: "" },
  VerifyChannelID: { type: String, default: "" },
  VerifyRoleID: { type: String, default: "" },
  Prefix: { type: String, default: "v-" },
  GuildToken: { type: String, default: "" },
  HasGeneratedToken: { type: Number, default: 0 },
  VerificationLevel1: { type: Boolean, default: false },
  VerificationLevel2: { type: Boolean, default: false },
  VerificationLevel3: { type: Boolean, default: false },
  VerifcationRBLX: { type: Boolean, default: false },
  VerificationRBLXGameLink: { type: Boolean, default: false },
  VerifyMessageWelcomeID: { type: String, default: "" },
  GuildName: { type: String, default: "" },
  GuildInvite: { type: String, default: "" },
  UserID: { type: String, default: "" },
  RobloxID: { type: String, default: "" },
  IsPendingVerification: { type: String, default: "" },
  IsNowVerified: { type: String, default: "" }
});

module.exports = mongoose.model("Eco", productSchema, "ecos");

也就是mongodb数据库ecos的模块导出。

【问题讨论】:

  • 你需要为updateOne方法提供两个参数,filter和update命令。 mongoosejs.com/docs/api.html#query_Query-updateOne
  • 那么我的第二个参数是什么?
  • 你能显示你的schemas/ecos文件的内容吗?
  • 好的,现在可以了!
  • 完成了,现在可以概览一下了

标签: database mongodb mongoose


【解决方案1】:

您的代码有点混乱,所以我更改了一些名称,但功能保持不变:

const mongoose = require("mongoose");
const Eco = require("../schemas/ecos");

module.exports = async(req, res) => {
  const document = await Eco.findOne({
      RobloxID: req.query.roblox_id
  });
  if(document.IsPendingVerification === "true") {
    document.IsNowVerified = "true"
    await document.save()
    res.send({
      success: true
    })
  }

现在我将完成更改:

const Eco = require("../schemas/ecos");

您要导出的不是架构,而是model。模型是定义数据库ecos 集合中的条目是什么样子的类。您可能有多个模型,因此最好正确命名它们。

  const document = await Eco.findOne({
      RobloxID: req.query.roblox_id
  });

findOne 查询返回的是模型的一个实例,代表集合中的document。这包含数据库文档中的所有信息,以及许多帮助您修改它的有用方法。

    document.IsNowVerified = "true"
    await document.save()

你可以直接修改这个文件,然后save它到数据库。这会使用您对其所做的任何更改来更新数据库中的文档。

updateOne直接用在模型上,例如:

    await Eco.updateOne({
      RobloxID: req.query.roblox_id
    }, {
      IsNowVerified = "true"
    })

这会通过RobloxId找到文档,然后更新第二个对象中传递的字段,在本例中为IsNowVerified 这很有用,因为它是单个数据库查询,所以很快。但是由于要先检查另一个字段,最好先找到该文档,然后再更新它。


另一件事是将"true" 更改为true。这将在 DB 中使用 Boolean 而不是 string,这会占用更少的空间,并且是标准做法,这将导致更少的错误。但是由于您已经在使用"true" 版本,因此更改它可能会导致很多错误,除非您到处更改它。

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多