Home > Mobile >  MongoDb Add field in array only is the arry is not null
MongoDb Add field in array only is the arry is not null

Time:09-30

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?

CodePudding user response:

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"
        ]
      }
    }
  }
])
  • Related