Ich versuche, ein Element aus einem Array in der MongoDB zu holen. Ich denke, dass der Aggregationsfilter der richtige ist. Aber ich habe es schon millionenfach versucht, ich kann immer noch nicht finden, wo das Problem liegt. Kannst du mir die Hand geben?Filter-Array in Filialdokument Feld
MongoDB Beispieldaten:
{
"_id" : 12,
"items" : [
{
"columns" : [
{
"title" : "hhh",
"value" : 10
},
{
"title" : "hahaha",
"value" : 20
}
]
},
{
"columns" : [
{
"title" : "hiii",
"value" : 50
}
]
}
]
}
Meine Lösung:
db.myCollection.aggregate([
{
$project: {
items: {
$filter: {
input: "$items",
as: "item",
cond: { $eq: [ "$$item.columns.title", "hahaha" ]}
}
}
}
}
]).pretty()
Mein Ergebnis:
{
"_id" : 15,
"items" : [
{
"columns" : [ ]
},
{
"columns" : [ ]
}
]
}
Erwartetes Ergebnis:
{
"_id" : 15,
"items" : [
{
"columns" : [
{
"title" : "hahaha",
"value" : 20
}
]
},
{
"columns" : []
}
]
}
Ich habe die Referenz Mongo geprüft: https://docs.mongodb.com/manual/reference/operator/aggregation/filter/#example
MongoDB Version: 3.4.1
Testumgebung: Mongo Shell