2016-03-20 185 views
-1

我有我的蒙戈DB這樣的數據:流星聚合。蒙戈DB

{ "_id" : "uhNTGacKPbnykv99S", "text" : "Task1", "createdAt" : ISODate("2016-03-20T10:29:59.669Z"), "owner" : "RE349TrQXoBm32mbr", "username" : "AngryRoot", "private" : false, "checked" : false } 
{ "_id" : "PsX7ZvyDoD26Kr78X", "text" : "Task2", "createdAt" : ISODate("2016-03-20T10:30:01.598Z"), "owner" : "RE349TrQXoBm32mbr", "username" : "AngryRoot", "private" : false, "checked" : false } 
{ "_id" : "mXpPWJ93GjugNCyME", "text" : "Task3", "createdAt" : ISODate("2016-03-20T10:30:15.899Z"), "owner" : "FbcQxF9q5a3t3LG9i", "username" : "CandyOgre", "private" : false, "checked" : false } 
{ "_id" : "WciNvRpZNAeiHNGhz", "text" : "Task4", "createdAt" : ISODate("2016-03-20T10:30:18.785Z"), "owner" : "FbcQxF9q5a3t3LG9i", "username" : "CandyOgre", "private" : false, "checked" : false } 

從蒙戈控制檯db.tasks.find()得到它。

我想返回集合,其中來自當前所有者的任務將優先。

例如。如果我以AngryRoot身份登錄,我想先顯示AngryRoot的任務,然後再顯示來自其他用戶的其他任務。我想我需要使用聚合。安裝此軟件包:meteorhacks:aggregate

我所有的嘗試都失敗了,如果可以的話,幫助我。 和一個文件,我需要彙總:enter link description here(在11原)。

感謝您花時間對我來說

回答

0

我沒有過流星命令,但你絕對可以通過拼接夫婦階段的聚集管道做到這一點使用聚合框架。

db.tasks.aggregate([ 
    // project a field showing if current user is owner 
    {$project: 
     {text:1, createdAt:1, owner:1, username:1, private:1, checked:1, current: 
     {$eq: ["$username", "AngryRoot"]} 
     } 
    }, 
    // Sort stage: Brings documents on top with current = true 
    {$sort: 
     {current:-1} 
    }, 
    // Optional!. Remove field name 'current' induced in first stage 
    {$project: 
     {text:1, createdAt:1, owner:1, username:1, private:1, checked:1} 
    } 
]) 

這應該打印記錄(假設AngryRoot是當前用戶)

{ 
    "_id" : "uhNTGacKPbnykv99S", 
    "text" : "Task1", 
    "createdAt" : ISODate("2016-03-20T10:29:59.669+0000"), 
    "owner" : "RE349TrQXoBm32mbr", 
    "username" : "AngryRoot", 
    "private" : false, 
    "checked" : false 
} 
{ 
    "_id" : "PsX7ZvyDoD26Kr78X", 
    "text" : "Task2", 
    "createdAt" : ISODate("2016-03-20T10:30:01.598+0000"), 
    "owner" : "RE349TrQXoBm32mbr", 
    "username" : "AngryRoot", 
    "private" : false, 
    "checked" : false 
} 
{ 
    "_id" : "mXpPWJ93GjugNCyME", 
    "text" : "Task3", 
    "createdAt" : ISODate("2016-03-20T10:30:15.899+0000"), 
    "owner" : "FbcQxF9q5a3t3LG9i", 
    "username" : "CandyOgre", 
    "private" : false, 
    "checked" : false 
} 
{ 
    "_id" : "WciNvRpZNAeiHNGhz", 
    "text" : "Task4", 
    "createdAt" : ISODate("2016-03-20T10:30:18.785+0000"), 
    "owner" : "FbcQxF9q5a3t3LG9i", 
    "username" : "CandyOgre", 
    "private" : false, 
    "checked" : false 
} 
+0

但是,如何能夠更好地在流星組織?我可以使用meteorhacks:聚合包,但我只能在服務器上使用它。我應該爲此還是如何創建新方法? –

+0

@МаксимРоманюк不幸的是我對流星一無所知,所以無法提出任何建議。但我想有一個單獨的方法來包裝聚合不是一個壞主意。 – Saleem