在通過集合框架管道化各種文檔後,我終於得到了生成的文檔。
現在的問題是,我只想要$ first和$ last文檔。
最後文件看起來像這樣(巨大的名單):
...
{
"_id" : "Kaila Deibler",
"count" : 406
},
{
"_id" : "Jessika Dagenais",
"count" : 406
},
{
"_id" : "Tamika Schildgen",
"count" : 404
},
...
的,我用來製作文檔蒙戈shell命令是:
db.talks.aggregate([{$project: {_id: 0, comments: "$comments"}}, {$unwind: "$comments"}, {$group: {_id: "$comments.author", count: {$sum: 1}}}, {$sort: {count: -1}}])
但我只需要第一個和最後一個所以我試圖這樣的事情:
db.talks.aggregate([{$project: {_id: 0, comments: "$comments"}}, {$unwind: "$comments"}, {$group: {_id: "$comments.author", count: {$sum: 1}}}, {$sort: {count: -1}}, {$first: "$_id"}])
我試過其他的實現,但似乎無法無花果排除何時/在哪裏執行$first
和$last
有什麼建議嗎?
你想在結果的「_id」字段中出現什麼?看起來你正試圖再次顯示「comments.author」(現在它將出現在$ _id中)字段),但在上下文中沒有多大意義,你可以給它一個任意的名字,比如'{$ group:{_id:「First_and_Last」,...' –
對,我忘了你可以這麼做。謝謝! – user1460015