2
我有一個文檔,必須根據其子文檔中的值進行搜索並按相關性排序。MongoDB通過匹配子文檔的相關性進行排序
在研究此主題時,我遇到了這個question。我現在知道我必須使用聚合框架,但解決方案不包括子文檔。
舉例來說,我想要抓住每個帶有teams.name ['blue','red','green']的子文檔的文檔,並根據它所匹配的大多數進行排序。
League Collection:
{
coach: Henderson,
teams:
[
{
name: red,
logo: bird,
division: east
},
{
name: blue,
logo: bluejay,
division: west
},
{
name: green,
logo: monkey,
division: east
}
]
}
{
coach: Wilkins,
teams:
[
{
name: red,
logo: bird,
division: east
},
{
name: blue,
logo: bluejay,
division: west
},
]
}
{
coach: Sandy,
teams:
[
{
name: red,
logo: bird,
division: east
}
]
}
最終的結果將是:
item total matches
-------- --------------
Henderson 3
Wilkins 2
Sandy 1
我將如何實現這一目標?