2
MongoDBの配列から要素を取得しようとしています。私は、集約フィルタが適用するのが正しいと思います。しかし、何百万回も試してみましたが、どこに問題があるのかまだ分かりません。私に手を差し伸べてもらえますか?サブディレクトリ配列フィールドのフィルタ配列
MongoDBのサンプルデータ:
{
"_id" : 12,
"items" : [
{
"columns" : [
{
"title" : "hhh",
"value" : 10
},
{
"title" : "hahaha",
"value" : 20
}
]
},
{
"columns" : [
{
"title" : "hiii",
"value" : 50
}
]
}
]
}
私のソリューション:
db.myCollection.aggregate([
{
$project: {
items: {
$filter: {
input: "$items",
as: "item",
cond: { $eq: [ "$$item.columns.title", "hahaha" ]}
}
}
}
}
]).pretty()
マイ結果:
{
"_id" : 15,
"items" : [
{
"columns" : [ ]
},
{
"columns" : [ ]
}
]
}
期待される結果:
{
"_id" : 15,
"items" : [
{
"columns" : [
{
"title" : "hahaha",
"value" : 20
}
]
},
{
"columns" : []
}
]
}
私はMongoの参照をチェックしています https://docs.mongodb.com/manual/reference/operator/aggregation/filter/#example
MongoDBのバージョン:3.4.1
テスト環境:あなたがサブ文書内のサブ配列を$filter
する$map
配列演算子を使用する必要がモンゴシェル