1

I am trying to add one property if current user has permission or not based on email exists in array of objects.

My input data looks like below.

[
  {
    nId: 0,
    children0: [
      {
        nId: 3,
        access: [
          {
            permission: "view",
            email: "[email protected]"
          }
        ]
      },
      {
        nId: 4,
        access: [
          {
            permission: "view",
            email: "[email protected]"
          }
        ]
      }
    ]
  }
]

https://mongoplayground.net/p/xZmRGFharAb

[
  {
    "$addFields": {
      "children0": {
        "$map": {
          "input": "$children0.access",
          "as": "accessInfo",
          "in": {
            "$cond": [
              {
                "$eq": [
                  "$$accessInfo.email",
                  "[email protected]"
                ]
              },
              {
                "$mergeObjects": [
                  "$$accessInfo",
                  {
                    "hasAccess": true
                  }
                ]
              },
              {
                "$mergeObjects": [
                  "$$accessInfo",
                  {
                    "hasAccess": false
                  }
                ]
              },
              
            ]
          }
        }
      }
    }
  }
]

I also tried this answer as following, but that is also not merging the object.

https://mongoplayground.net/p/VNXcDnXl_sZ

1 Answer 1

1

Try this:

db.collection.aggregate([
  {
    "$addFields": {
      "children0": {
        "$map": {
          "input": "$children0",
          "as": "accessInfo",
          "in": {
            nId: "$$accessInfo.nId",
            access: "$$accessInfo.access",
            hasAccess: {
              "$cond": {
                "if": {
                  "$ne": [
                    {
                      "$size": {
                        "$filter": {
                          "input": "$$accessInfo.access",
                          "as": "item",
                          "cond": {
                            "$eq": [
                              "$$item.email",
                              "[email protected]"
                            ]
                          }
                        }
                      }
                    },
                    0
                  ]
                },
                "then": true,
                "else": false
              }
            }
          }
        }
      }
    }
  }
])

Here, we use one $map to loop over children0 and then we filter the access array to contain only elements with matching emails. If the filtered array is non-empty, we set hasAccess to true.

Playground link.

Sign up to request clarification or add additional context in comments.

3 Comments

that gives hasAccess inside access array, we need that in parallel to 'access' key. so if user exists in array then it will be like { nId: 3, hasAccess: true }
Phew. Thanks a lot man. You are an angel. Spent entire day after this.
Cool. Take a chill pill now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.