mongodb Aggregation聚合操作之$sortByCount

在上一篇mongodb Aggregation聚合操作之$sort中詳細(xì)介紹了mongodb聚合操作中的$sort使用以及參數(shù)細(xì)節(jié)。本篇將開(kāi)始介紹Aggregation聚合操作中的$sortByCount操作。

說(shuō)明:

???根據(jù)指定表達(dá)式的值對(duì)傳入文檔進(jìn)行分組,然后計(jì)算每個(gè)不同組中的文檔數(shù)。每個(gè)輸出文檔包含兩個(gè)字段:一個(gè)_id字段包含不同的分組值,一個(gè)count字段包含屬于該分組或類別的文檔數(shù)。文檔按count降序排序。

語(yǔ)法:

???{ ?$sortByCount :?< expression > ?}


備注:$sortByCount階段等價(jià)于下面的$group + $sort序列

{ $group: { _id: <expression>, count: { $sum: 1 } } },

{ $sort: { count: -1 } }


參數(shù)講解:

?? expression:表達(dá)式分組依據(jù)。您可以指定除文檔文字之外的任何表達(dá)式。要指定字段路徑,請(qǐng)?jiān)谧侄蚊Q前加一個(gè)美元符號(hào)$,并將其括在引號(hào)中。例如,要按employee字段分組,請(qǐng)指定"$employee"為表達(dá)式:{ ?$ sortByCount :?“ $ employee” ?}

雖然不能為group by expression指定文檔文字,但是可以指定計(jì)算結(jié)果為文檔的字段或表達(dá)式。例如,如果employee 和business 字段是文檔字段,那么下面的$mergeObjects表達(dá)式(計(jì)算結(jié)果為文檔)是$sortByCounts的一個(gè)有效參數(shù):

{ $sortByCount: { $mergeObjects: [ "$employee", "$business" ] } }

但是,下面這個(gè)文檔文字表達(dá)式的例子是無(wú)效的:

{ $sortByCount: { lname: "$employee.last", fname: "$employee.first" } }

1.?示例


初始化數(shù)據(jù):

db.sortByCountExample.insertMany([{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, "tags" : [ "painting", "satire", "Expressionism", "caricature" ] },

{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, "tags" : [ "woodcut", "Expressionism" ] },

{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, "tags" : [ "oil", "Surrealism", "painting" ] },

{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", "tags" : [ "woodblock", "ukiyo-e" ] },

{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, "tags" : [ "Surrealism", "painting", "oil" ] },

{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, "tags" : [ "oil", "painting", "abstract" ] },

{ "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, "tags" : [ "Expressionism", "painting", "oil" ] },

{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, "tags" : [ "abstract", "painting" ] }])


示例:下面的操作$unwinds展開(kāi)標(biāo)簽數(shù)組,并使用$sortByCount階段來(lái)計(jì)算與每個(gè)標(biāo)簽關(guān)聯(lián)的文檔數(shù)量


db.sortByCountExample.aggregate( [ { $unwind: "$tags" }, ?{ $sortByCount: "$tags" } ] )


結(jié)果:

{ "_id" : "painting", "count" : 6 }

{ "_id" : "oil", "count" : 4 }

{ "_id" : "Expressionism", "count" : 3 }

{ "_id" : "Surrealism", "count" : 2 }

{ "_id" : "abstract", "count" : 2 }

{ "_id" : "woodblock", "count" : 1 }

{ "_id" : "woodcut", "count" : 1 }

{ "_id" : "ukiyo-e", "count" : 1 }

{ "_id" : "satire", "count" : 1 }

{ "_id" : "caricature", "count" : 1 }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容