In my MongoDB aggregation pipeline, I want to retrieve the matching objects for a number from the data (see below)
My data is is like the below:
{
id: 123
contactsOfAppUsers:[
{
id:'abc',
contactsArray: ['9999911111','9999922222']
},
{
id:'efg',
contactsArray: ['9999933333','9999944444']
},
{
id:'hij',
contactsArray: ['9999955555','9999933333']
}
]
}
When I search for this “9999933333” in the above data, I would like the result like this:
'matchingObjects':[
{
id:'efg',
phoneNumbers: ['9999933333','9999944444']
},
{
id:'hij',
phoneNumbers: ['9999933333','9999955555']
}
]
I tried this, which gives boolean values but I actually want the matching objects (as shown above)
db.phNumbers.aggregate([
{// Previous stage},
{
$addFields: {
'matchingObjects': {
'$map': {
'input': '$contactsOfAppUsers',
'as': 'cc',
'in': {
'$in': [
'9999933333','$cc.contactsArray'
]
}
}
}
}
},
])