{
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.