I have this document in the MongoDB with the following structure stored inside result field
[
{
"id" : "131429",
"categoryId" : "3",
"results" : [{
"id" : "yAHPPvo",
"supplierId" : 31,
"countryId" : "91",
"cityId" : "91",
}, {
"id" : "BAYYESB",
"supplierId" : 31,
"countryId" : "91",
"cityId" : "91",
}]
},
{
"id" : "22534",
"categoryId" : "2",
"results" : [{
"id" : "DerrDSQ",
"supplierId" : 14,
"countryId" : "91",
"cityId" : "91",
}, {
"id" : "TTerySQ",
"supplierId" : 5,
"countryId" : "91",
"cityId" : "91",
}]
}
]
Trying to figure out how to query and extract the object with key of "yAHPPvo", figured the first part to find the necessary document, but how do I extract the sub-object inside of it? I've seen on the Net that this is done with aggregation functions and unwinding, but just cannot seem to put my finger on it. I can convert it to an array if it makes it easier, but have tried both with little success.
Update
This is what I've tried so far from the duplicate link suggestion:
db.getCollection('searches').aggregate([
{$match: {'result.id': '131429'}},
{$project: {
result: {$filter: {
input: '$result',
as: 'result',
cond: {$eq: ['$$result.id', 'yAHPPvo']}
}},
_id: 0
}}
])
but that returns 0 records.