Given that I have a hash subdocument within my document, how can I match a value – categoryId
(itself added using $addFields
) – to the sub-document key – categoryTypes
– in my aggregation pipeline:
ex:
{
_id: $someId,
categoryId: 2,
categoryTypes: {
1: "hello world",
2: "something else",
3: "still more stuff"
}
}
I need to match categoryId
(2) to key in categoryTypes
(2). In essence I need to say:
$match: { categoryId: categoryTypes }
Thanks!
2
Answers
Well it turns out that's simpler than I originally thought:
1- First changed the hash into an array which produced a subdocument which looks like
ARRAY: [ "0": {...}, "1": {...}, ...]
2- Then in my
project
stage I did:Hope this helps others who may be looking for a similar solution.
One option is to use
$objectToArray
:See how it works on the playground example