【问题标题】:MongoDb Add field in array only is the arry is not nullMongoDb 仅当数组不为空时才在数组中添加字段
【发布时间】:2021-11-21 20:46:57
【问题描述】:

现在我有了新情况3.0版。 我有这个假的 json:

[
   {
      "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
         }
      ]
   },
   {
      "type":"PF",
      "code":12345,
      "Name":"Anakin Skywalker",
      "currency":"BRL",
      "status":"ACTIVE",
      "localization":"NABOO",
      "createDate":1627990848665,
      "olderAdress":null
   }
]

我需要在每个数组元素中添加一个新字段仅当数组不为空时。但我需要通过聚合来做到这一点,因为我在发送给用户之前在 Spring 中使用了这个结果。

我需要这个结果:

[
  {

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

所以我只在 olderAddress 不为 null 和 oldAddress 为 null 的地方添加了字段 isItemOfOlderAddress 我只显示默认信息。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    查询

    • 如果 oldAdress 是 array(所以也不是 null),将 "isItemOfOlderAddress": true 字段添加到所有成员
    • 否则保留旧值(所以也保留null

    Test code here

    db.collection.aggregate([
      {
        "$set": {
          "olderAdress": {
            "$cond": [
              {
                "$isArray": [
                  "$olderAdress"
                ]
              },
              {
                "$map": {
                  "input": "$olderAdress",
                  "in": {
                    "$mergeObjects": [
                      "$$this",
                      {
                        "isItemOfOlderAddress": true
                      }
                    ]
                  }
                }
              },
              "$olderAdress"
            ]
          }
        }
      }
    ])
    

    【讨论】:

    • 对不起,如果我的问题是初学者,但我可以使用聚合来做到这一点,因为正如我告诉你的那样,我的管道中还有其他步骤......
    • 没关系,我更新了它,它是聚合更新,所以很容易
    • Takis,再次感谢您。不是所有的英雄都穿斗篷...你是最棒的!!!!!
    • 不是真正的英雄,但很高兴我提供了帮助,如果您喜欢 mongodb,请从文档中阅读参考资料非常好,还有示例see this
    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 2018-01-15
    • 2019-06-25
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多