MongoDB $unset 如果条件满足 [英] MongoDB $unset If condition is met

查看:33
本文介绍了MongoDB $unset 如果条件满足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Someone colud please help me with this situation?

我有这个假 JSON...

I have this fake 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",我想删除字段code.或取消".我使用聚合是因为我在 Practical Example 之前做了很多事情.我该怎么做??

I would like to remove the field code if the status equals "BLOCKED" or "CANCELLED". I'm using aggregate because I'm doing a lot of things before Practical Example. How can I do that??

我需要这个结果:

[
  {
    "_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.

Soo...Independent of the name, I will check the status and if you contemplate the condition I will remove the field code.

推荐答案

查询

  • 使用系统变量 $$REMOVE 如果字段获取此值并将其删除
  • 所以条件是 user.code ,如果不是 BLOCKED",CANCELLED" ,则保留旧值,否则 $$REMOVE"字段
  • uses a system variable $$REMOVE if a field gets this value its removed
  • so the condition is user.code , keep old value if not "BLOCKED","CANCELLED" , else "$$REMOVE" the field

在这里测试代码

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


编辑

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

The above code checks the user.status but you want remove code or not based onthe user.olderAdress.status (after the unwind) (its 2 fields with same name status)

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

Query (add this after the stages you already have)

测试代码

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

这篇关于MongoDB $unset 如果条件满足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆