0
考慮下面兩個文件:組數組元素計數出現
{ "name" : "test1", "source" : ["Apple","Cherry"], "dest" : ["Banana", "Durian"]}
{ "name" : "test2", "source" : ["Melon","Fig"], "dest" : ["Apple"]}]
輸入
["Apple","Orange","Fig"]
期望輸出
{ "_id" : "Apple", nameList : ["test1", "test2"] }
{ "_id" : "Fig", nameList : ["test2"] }
{ "_id" : "Orange", nameList : [] }
聚合
db.getCollection<name>.aggregate([
{ "$match" : <some_matching_conditions>},
{ "$project" :
{ "$project" : { "input_array" : { "$literal" : ["Apple","Orange","Fig"]} }},
{ "obj" : {"$setUnion" : ["$source", "$dest"]}},
},
{ "$unwind" : "$aray"},
{ "$match" : { "aray" : { "$in" : "$obj"}}},
{ "$group" : <grouping_conditons>}
])
錯誤:「errmsg」:「異常:錯誤查詢:BadValue $ in需要數組」 – Bala