I have the following issue when I try to match elements in an array based on the value in one of the array subfields.
Example document structure:
{
"A" : {
"C" : "abc"
},
"B" : [
{
"C" : "def"
},
{
"C" : "ghi"
},
{
"C" : "jkl"
},
{
"C" : "abc"
}
]
}
Example result document:
{
"A" : {
"C" : "abc"
},
"B" : [
{
"C" : "abc"
}
]
}
My attempt:
db.collection.aggregate([
{'$match': {
'B.C': 'A.C'
}},
{'$project': {
'A.C': 1,
'B.C': 1
}}
])
Where am I making an error?
Thanks!