0
下面是示例MongoDB的數據模型的一個user
集合:複雜聚合查詢
{
"_id": ObjectId('58842568c706f50f5c1de662'),
"userId": "123455",
"user_name":"Bob"
"interestedTags": [
"music",
"cricket",
"hiking",
"F1",
"Mobile",
"racing"
],
"listFriends": [
"123456",
"123457",
"123458"
]
}
listFriends
是userId
爲其他用戶的陣列
對於特定userId
我需要提取listFriends
(userId
的)和那些userId
的我需要聚合interestedTags
和他們的計數。
我將能夠通過將查詢實現爲兩個部分是:
1)提取listFriends
特定userId
,
2)在aggregate()
功能,一些使用此列表這樣
db.user.aggregate([
{ $match: { userId: { $in: [ "123456","123457","123458" ] } } },
{ $unwind: '$interestedTags' },
{ $group: { _id: '$interestedTags', countTags: { $sum : 1 } } }
])
我試圖解決的問題是:有沒有辦法實現上述功能在一個單一的聚合函數(2兩個步驟1和)?