问题
Let's say I have the following document structure:
{
"A": {
"_id": "ID0"
},
"B": [
"a": {
"_id": "ID0",
"field": "X"
},
"b": {
"_id": "ID1",
"field": "Y"
}
]
}
I want to project B
matched with the _id
in A
. The end result would be:
{
"B": [
"a": {
"_id": "ID0",
"field": "X"
}
]
}
I tried the following but apparently I'm doing something wrong:
db.collection.aggregate([
{$match: {"B._id": "$A._id"}},
{$project: {"B": 1}}
])
来源:https://stackoverflow.com/questions/61223895/mongodb-match-array-based-on-document-value