0
我對MongoDB有一個簡單的社交網絡實現。我的架構看起來像這樣:使用MongoDB進行社交網絡查詢
User
_id
name
Friend
_id
user
friend
Post
_id
user
timestamp
text
我試圖使用aggregate
方法從我所有的朋友們最近發表的文章列表,但我想從同一個用戶羣一起連續帖子。因此,假設我有3個朋友,他們有以下職位:
{"user": "user1", timestamp: 1476200010, text: "hello1"}
{"user": "user2", timestamp: 1476200009, text: "hello2"}
{"user": "user2", timestamp: 1476200008, text: "hello3"}
{"user": "user3", timestamp: 1476200007, text: "hello4"}
{"user": "user2", timestamp: 1476200006, text: "hello5"}
{"user": "user2", timestamp: 1476200005, text: "hello6"}
{"user": "user1", timestamp: 1476200004, text: "hello7"}
{"user": "user1", timestamp: 1476200003, text: "hello8"}
我想作爲一個結果,獲得以下信息:
{"user": "user1", timestamp: 1476200010, text: "hello1"}
{"user": "user2", timestamp: 1476200009, text: "hello2"}
{"user": "user3", timestamp: 1476200007, text: "hello4"}
{"user": "user2", timestamp: 1476200006, text: "hello5"}
{"user": "user1", timestamp: 1476200004, text: "hello7"}
我堅持我的查詢了一下,任何幫助表示讚賞:
db.Post.aggregate([
{$sort: {timestamp: -1}},
{$lookup: {from: 'Friend', localField: '_id', foreignField: 'friend', as: 'friend'}},
{$match: {'friend.user': current_user}},
???
{$limit: 100},
])
關於這個問題,'aggregate'沒有任何支持涉及到鄰居文檔的操作,所以我認爲你需要在你的應用程序代碼中進行連續的post修剪。 – JohnnyHK