MongoDB Compass filter dynamic array

81 Views Asked by At

I am trying for MongoDB to filter based on below json format for serviceCountry = US. I only want to return those gfcidGlobals records and their corresponding object that reflects serviceCountry = US. All over objects can be ignored. Ideally I will return the gfcidGlobals and the corresponding details with the serviceCountry.

JSON:

[
  {
    "_id": {
      "$oid": "648c0875a25a3a13f1e3e7cf"
    },
    "eventId": {
      "$oid": "648c0875a25a3a13f1e3e7ce"
    },
    "gfcidGlobals": {
      "789456489": [
        {
          "className": "test123",
          "gfcid": "789456489",
          "companyName": "ABC BRANCH",
          "serviceCountry": "UNITED KINGDOM",
          "kwtPolicyDueDate": {
            "$date": {
              "$numberLong": "1699833600000"
            }
          },
          "kycLobOwner": "BSU",
          "clientType": "BANK"
        },
        {
          "className": "test123",
          "gfcid": "789456489",
          "companyName": "ABC BRANCH",
          "serviceCountry": "US",
          "kwtPolicyDueDate": {
            "$date": {
              "$numberLong": "1699833600000"
            }
          },
          "kycLobOwner": "BSU",
          "clientType": "BANK"
        },
        {
          "className": "test123",
          "gfcid": "789456489",
          "companyName": "ABC BRANCH",
          "serviceCountry": "GERMANY",
          "kwtPolicyDueDate": {
            "$date": {
              "$numberLong": "1699833600000"
            }
          },
          "kycLobOwner": "BSU",
          "clientType": "BANK"
        }
      ],
      "456789412": [
        {
          "className": "test123",
          "gfcid": "456789412",
          "companyName": "FIRST BANK CORP",
          "serviceCountry": "PHILIPPINES",
          "kwtPolicyDueDate": {
            "$date": {
              "$numberLong": "1699833600000"
            }
          },
          "kycLobOwner": "BSU",
          "clientType": "NONBANK"
        },
        {
          "className": "test123",
          "gfcid": "456789412",
          "companyName": "FIRST BANK CORP",
          "serviceCountry": "Philippines",
          "kwtPolicyDueDate": {
            "$date": {
              "$numberLong": "1699833600000"
            }
          },
          "kycLobOwner": "BSU",
          "clientType": "NONBANK"
        }
      ]
    }
  }
]
1

There are 1 best solutions below

0
Wernfried Domscheit On

I think you are looking for this one:

db.collection.aggregate([
   { $set: { ids: { $objectToArray: "$ids" } } },
   {
      $set: {
         ids: {
            $map: {
               input: "$ids",
               as: "item",
               in: {
                  k: "$$item.k",
                  v: {
                     $filter: {
                        input: "$$item.v",
                        cond: { $eq: ["$$this.country", 'US'] }
                     }
                  }
               }
            }
         }
      }
   },
   { $set: {ids: {$arrayToObject: "$ids"}} }
])

Mongo Playground