【问题标题】:MongoDB $unset If condition is metMongoDB $unset 如果条件满足
【发布时间】:2021-11-11 11:56:22
【问题描述】:

请有人帮我解决这种情况?

我有这个假 JSON...

[
  {
    "user": {
      "type": "PF",
      "code": 12345,
      "Name": "Darth Vader",
      "currency": "BRL",
      "status": "ACTIVE",
      "localization": "NABOO",
      "createDate": 1627990848665,
      "olderAdress": [
        {
          "localization": "DEATH STAR",
          "status": "BLOCKED",
          "createDate": 1627990848665
        },
        {
          "localization": "TATOOINE",
          "status": "CANCELLED",
          "createDate": 1627990555665
        },
        {
          "localization": "ALDERAAN",
          "status": "INACTIVED",
          "createDate": 1627990555665
        },
        
      ]
    }
  }
]

如果状态等于“BLOCKED”或“CANCELLED”,我想删除字段code。我使用聚合是因为我在Practical Example 之前做了很多事情。我该怎么做??

我需要这个结果:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "user": {
      "Name": "Darth Vader",
      "createDate": 1.627990848665e+12,
      "currency": "BRL",
      "localization": "DEATH STAR",
      "status": "BLOCKED",
      "type": "PF"
    }
  },
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "user": {
      "Name": "Darth Vader",
      "createDate": 1.627990555665e+12,
      "currency": "BRL",
      "localization": "TATOOINE",
      "status": "CANCELLED",
      "type": "PF"
    }
  },
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "user": {
      "Name": "Darth Vader",
      "code": 12345,
      "createDate": 1.627990555665e+12,
      "currency": "BRL",
      "localization": "ALDERAAN",
      "status": "INACTIVED",
      "type": "PF"
    }
  },
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "user": {
      "Name": "Darth Vader",
      "code": 12345,
      "createDate": ISODate("2021-09-16T17:36:26.405Z"),
      "currency": "BRL",
      "localization": "NABOO",
      "status": "ACTIVE",
      "type": "PF"
    }
  }
]

Soo...独立于名称,我将检查状态,如果您考虑条件,我将删除字段 code

【问题讨论】:

    标签: json mongodb spring-data-jpa aggregation-framework unset


    【解决方案1】:

    查询

    • 使用系统变量 $$REMOVE 如果字段获取此值并将其删除
    • 所以条件是 user.code ,如果不是 "BLOCKED","CANCELLED" 则保留旧值,否则 "$$REMOVE" 字段

    Test code here

    db.collection.aggregate([
      {
        "$set": {
          "user.code": {
            "$cond": [
              {
                "$in": [
                  "$user.status",
                  [
                    "BLOCKED",
                    "CANCELLED"
                  ]
                ]
              },
              "$$REMOVE",
              "$user.code"
            ]
          }
        }
      }
    ])
    

    编辑

    上面的代码检查user.status,但是你想删除代码还是不基于user.olderAdress.status(在展开之后) (其 2 个字段具有相同的名称状态)

    查询(在您已经拥有的阶段之后添加)

    Test code

    {
        "$set": {
          "user.code": {
            "$cond": [
              {
                "$in": [
                  "$user.status",
                  [
                    "BLOCKED",
                    "CANCELLED"
                  ]
                ]
              },
              "$$REMOVE",
              "$user.code"
            ]
          }
        }
      }
    

    【讨论】:

    • 部分没问题...问题是...正如我之前写的,我还有其他 piperlines (mongoplayground.net/p/QehOjXOEpbz) 我不确定我什么时候介绍这个 $set...跨度>
    • 看来你可以先加上你已经拥有的$addfields,set=addfields,见this
    • 但是如果你在发送给我的时候运行这个例子......你可以看到每个寄存器的“代码”都被删除了......不仅仅是条件......
    • 是的,但这不是预期的吗? "Name": "Darth Vader" 是 status : "BLOCKED" ,所以在他的所有地址中都没有代码字段。请参阅this 用户 Darth Vader2 仍有其代码,因为状态为“活动”
    • 它有 2 个名为 status 的字段,我看到了嵌套的字段,现在好了,您想在展开后更改数据,基于嵌套状态而不是顶级状态,我认为这是时间会好的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2018-08-24
    • 2019-07-19
    • 1970-01-01
    • 2019-03-01
    • 2020-06-19
    • 1970-01-01
    相关资源
    最近更新 更多