1

I have the following document structure:

{
    "user_id": "102",
    "roles": [{
        "name": "superuser"
    }, {
        "name": "admin"
    }, {
        "name": "account_admin"
    }]
}

And I need to unwind into the following result:

{
    "user_id": "102290863723817866607",
    "roles": [
        0: "superuser",
        1: "gsuite_admin",
        2: "account_admin"
    ]
}

I can't quite figure out how to stage the aggregation pipeline to get this result

1 Answer 1

3

You can use a $projectcompined with a $map to select the name from your role`s object:

db.collection.aggregate([
  {
    "$project": {
      "roles": {
        "$map": {
          "input": "$roles",
          "as": "role",
          "in": "$$role.name"
        }
      }
    }
  }
])
Sign up to request clarification or add additional context in comments.

Comments

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.