【问题标题】:if else condition based update status in mongodbif else MongoDB中基于条件的更新状态
【发布时间】:2021-05-15 08:04:07
【问题描述】:

没有任何其他解决方案..

findOneAndUpdate( {"_id": ObjectId("5714ce0a4514ef3ef68677fd")}, $cond: { if: { $eq: [ "$isRequired", true ] }, then: { "status": "Done" }, else: { { "status": "Approved" } } )

基于isRequire的相同采集状态更新是真还是假。

【问题讨论】:

  • 嗨!欢迎来到 StackOverflow。您能否向我们提供有关问题所在、您尝试过的内容以及代码的更多上下文的更多详细信息?请查看How do i ask a good question 以帮助您的同胞。

标签: node.js mongodb typescript if-statement mongodb-query


【解决方案1】:

首先是您的查询语法错误,您在查询查询对象之后缺少{}

更新查询由:update({query}, {update}) 组成,而您已写入:update( {query}, update)

此外,您不能在更新查询中使用$cond,因为它是一个聚合阶段,但从 Mongo 4.2 开始,mongo 允许聚合阶段 (reference),因此您可以执行此查询:

db.collection.update({
  "_id": "5714ce0a4514ef3ef68677fd"
},
[
  {
    "$set": {
      "status": {
        "$cond": {
          if: {
            $eq: [
              "$isRequired",
              true
            ]
          },
          then: "Done",
          else: "Approved"
        }
      }
    }
  }
])

例如here 的值为假,here 的值为真

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 2015-04-21
    • 1970-01-01
    • 2020-12-29
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    相关资源
    最近更新 更多