MongoDb 仅在数组中添加字段是 arry 不为空 [英] MongoDb Add field in array only is the arry is not null

查看:68
本文介绍了MongoDb 仅在数组中添加字段是 arry 不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>[{类型":PF",代码":12345,名称":达斯维达",货币":BRL",状态":活动",本地化":NABOO",创建日期":1627990848665,旧地址":[{本地化":死亡之星",状态":已阻止",创建日期":1627990848665},{本地化":TATOOINE",状态":已取消",创建日期":1627990555665},{本地化":ALDERAAN",状态":未激活",创建日期":1627990555665}]},{类型":PF",代码":12345,名称":阿纳金·天行者",货币":BRL",状态":活动",本地化":NABOO",创建日期":1627990848665,旧地址":空}]

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

我需要这个结果:

<预><代码>[{类型":PF",代码":12345,名称":达斯维达",货币":BRL",状态":活动",本地化":NABOO",创建日期":1627990848665,旧地址":[{定位":死亡之星",状态":已阻止",创建日期":1627990848665,isItemOfOlderAddress": 真的},{本地化":塔图因",状态":已取消",创建日期":1627990555665,isItemOfOlderAddress": 真的},{本地化":ALDERAAN",状态":未激活",创建日期":1627990555665,isItemOfOlderAddress": 真的},]},{类型":PF",代码":12345,名称":阿纳金·天行者",货币":BRL",状态":活动",本地化":NABOO",创建日期":1627990848665,olderAdress":空},]

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

解决方案

查询

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

此处测试代码

db.collection.aggregate([{$set":{旧地址":{$cond":[{$isArray":[$olderAdress"]},{$地图":{输入":$olderAdress",在":{$mergeObjects":[$$this",{isItemOfOlderAddress":真}]}}},$olderAdress"]}}}])

Now I have a new situation Version 3.0. I have this fake 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
   }
]

And I need to add a new field in each array element ONLY IF THE ARRAY IS NOT NULL. But I need to do that by aggregate because I'm using this result in Spring before sending to the users.

I need this result:

[
  {

      "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
    },  
]

So I added the field isItemOfOlderAddress only where olderAddress is not null and where olderAddress is null I only show the default information. How can I do that?

解决方案

Query

  • if olderAdress is an array(so not null also), add "isItemOfOlderAddress": true field to all members
  • else keep the old value(so keep the null also)

Test code here

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

这篇关于MongoDb 仅在数组中添加字段是 arry 不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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