Aggregate the document

Each document in collection called personalities looks like this.

{
  "_id": "1",
  "mbti": "ENFJ",
  "mbti_description": "This is description",
  "mbti_relation": [
    {
      "relation": "best",
      "mbti": "ISTP"
    },
    {
      "relation": "worst",
      "mbti": "ISTJ"
    }]
}

I want to get the result like this.

{
  "_id": "1",
  "mbti": "ENFJ",
  "mbti_description": "ENFJ description",
  "mbti_relation": [
    {
      "relation": "best",
      "mbti": "ISTP",
      "mbti_description": "ISTP description",
    },
    {
      "relation": "worst",
      "mbti": "ISTJ",
      "mbti_description": "ISTJ description"
    }]
}

I want to get the "mbti_description" by "mbti" in the array, "mbti_relation"

I tried to use aggregate with $graphLookup or $addFields but couldn’t get result what I want.
I just want to get the details of each "mbti" in "mbti_relation" without breaking the constructor of it.

Can you give me little tips to implement it?

Thank you so much @turivishal !
It was really helpful to describe the each aggregation commands.

It’s little bit to say, but happy new year!

1 Like

* little bit late to say

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.