Aggregation Help

I am finding it difficult to output a document

{
  skillId: new ObjectId("61a914ac1155e2fb40e8d9c1"),
  test: [
    {
      skills:[
  {
    description: 'HTML presentation and formatting tags',
    _id: new ObjectId("61a914ac1155e2fb40e8d9c0")
  },
  {
    description: 'HTML layouts, using groups of elements together',
    _id: new ObjectId("61a914ac1155e2fb40e8d9c1")
  },
  {
    description: 'Advanced concepts',
    _id: new ObjectId("61a914ac1155e2fb40e8d9c2")
  
],
    }
  ],
}

//aggregation
 descriptions: {
                    $map: {
                        input: "$test.skills",
                        as: "skills",
                        in: {
                            $cond: [
                                {$eq: ["$$skills._id", "$skillId"]},
                                {$indexOfArray: ["$$skills", "$skillId"]},
                                null
                            ]
                        }
                    }
                },

I want a result that does the following thing it matches the Id of skillId with test.skills._id and return its description i wrote the above map but it doesn’t return anything null can someone suggest me proper way of query nested arrays

It is not clear what you want to achieve. It would be easier for us to understand if you supply the desired result that you want.

However, I see 2 major issues with your code.

1 - The array skills is within an object of the array test and you seem to process test as if it was an object.

You need to $map, $filter or $reduce the array test.

2 - You use $indexOfArray on the $map variable $$skills.

The variable $$skills represents 1 element of the input array. If you want to refer to the input array (which is wrong, see point 1) you need a single $ sign, just like you do with $skillId. The confusion might come from the fact that your code diverge from the habit of using plural for array and singular for an element of the array; like if the array is skills, each element is a skill.

Hi, steevej thank you for the response. I figured out how to get the desired output. I was looking for a document like this
image

The aggregation I wrote seems so messy

{
        $addFields: {
            idx: {
                $map: {
                    input: "$test.skills", as: "skill", in: {
                        index: {
                            $indexOfArray: ["$$skill._id", "$skillId",],
                        },
                    },
                },
            }, description: {
                $map: {
                    input: "$test.skills", as: "skill", in: {
                        des: "$$skill.description",
                    },
                },
            },
        },
    }, {
        $addFields: {
            index: {$arrayElemAt: ["$idx.index", 0]},
        },
    }, {
        $addFields: {
            descriptionzo: {
                $arrayElemAt: ["$description.des", 0],
            },
        },
    },
  {
    $project: {
      _id: 0,
      skillId: 1,
      "test.skills":1,
      description: {$arrayElemAt: ["$descriptionzo", '$index']},
    },
  },

can you please suggest me a more optimized way for achieving the description.
Thanks.

As already mentioned

How do you handle the test array? In your example, it only has 1 element. You should read about $reduce and $filter.

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