MongoDB數(shù)據(jù)庫功能強(qiáng)大!除了基本的查詢功能之外,還提供了強(qiáng)大的聚合功能。這里簡單介紹一下count、distinct和group。
1.count:
--在空集合中,count返回的數(shù)量為0。 > db.test.count() 0 --測試插入一個(gè)文檔后count的返回值。 > db.test.insert({"test":1}) > db.test.count() 1 > db.test.insert({"test":2}) > db.test.count() 2 --count和find一樣,也接受條件。從結(jié)果可以看出,只有符合條件的文檔參與了計(jì)算。 > db.test.count({"test":1}) 1
2.distinct:
distinct用來找出給定鍵的所有不同的值。使用時(shí)也必須指定集合和鍵。
--為了便于后面的測試,先清空測試集合。 > db.test.remove() > db.test.count() 0 --插入4條測試數(shù)據(jù)。請(qǐng)留意Age字段。 > db.test.insert({"name":"Ada", "age":20}) > db.test.insert({"name":"Fred", "age":35}) > db.test.insert({"name":"Andy", "age":35}) > db.test.insert({"name":"Susan", "age":60}) --distinct命令必須指定集合名稱,如test,以及需要區(qū)分的字段,如:age。 --下面的命令將基于test集合中的age字段執(zhí)行distinct命令。 > db.runCommand({"distinct":"test", "key":"age"}) { "values" : [ 20, 35, 60 ], "stats" : { "n" : 4, "nscanned" : 4, "nscannedObjects" : 4, "timems" : 0, "cursor" : "BasicCursor" }, "ok" : 1 }
3.group:
group做的聚合有些復(fù)雜。先選定分組所依據(jù)的鍵,此后MongoDB就會(huì)將集合依據(jù)選定鍵值的不同分成若干組。然后可以通過聚合每一組內(nèi)的文檔,產(chǎn)生一個(gè)結(jié)果文檔。
--這里是準(zhǔn)備的測試數(shù)據(jù) > db.test.remove() > db.test.insert({"day" : "2012-08-20", "time" : "2012-08-20 03:20:40", "price" : 4.23}) > db.test.insert({"day" : "2012-08-21", "time" : "2012-08-21 11:28:00", "price" : 4.27}) > db.test.insert({"day" : "2012-08-20", "time" : "2012-08-20 05:00:00", "price" : 4.10}) > db.test.insert({"day" : "2012-08-22", "time" : "2012-08-22 05:26:00", "price" : 4.30}) > db.test.insert({"day" : "2012-08-21", "time" : "2012-08-21 08:34:00", "price" : 4.01}) --這里將用day作為group的分組鍵,然后取出time鍵值為最新時(shí)間戳的文檔,同時(shí)也取出該文檔的price鍵值。 > db.test.group( { ... "key" : {"day":true}, --如果是多個(gè)字段,可以為{"f1":true,"f2":true} ... "initial" : {"time" : "0"}, --initial表示$reduce函數(shù)參數(shù)prev的初始值。每個(gè)組都有一份該初始值。 ... "$reduce" : function(doc,prev) { --reduce函數(shù)接受兩個(gè)參數(shù),doc表示正在迭代的當(dāng)前文檔,prev表示累加器文檔。 ... if (doc.time > prev.time) { ... prev.day = doc.day ... prev.price = doc.price; ... prev.time = doc.time; ... } ... } } ) [ { "day" : "2012-08-20", "time" : "2012-08-20 05:00:00", "price" : 4.1 }, { "day" : "2012-08-21", "time" : "2012-08-21 11:28:00", "price" : 4.27 }, { "day" : "2012-08-22", "time" : "2012-08-22 05:26:00", "price" : 4.3 } ] --下面的例子是統(tǒng)計(jì)每個(gè)分組內(nèi)文檔的數(shù)量。 > db.test.group( { ... key: { day: true}, ... initial: {count: 0}, ... reduce: function(obj,prev){ prev.count++;}, ... } ) [ { "day" : "2012-08-20", "count" : 2 }, { "day" : "2012-08-21", "count" : 2 }, { "day" : "2012-08-22", "count" : 1 } ] --最后一個(gè)是通過完成器修改reduce結(jié)果的例子。 > db.test.group( { ... key: { day: true}, ... initial: {count: 0}, ... reduce: function(obj,prev){ prev.count++;}, ... finalize: function(out){ out.scaledCount = out.count * 10 } --在結(jié)果文檔中新增一個(gè)鍵。 ... } ) [ { "day" : "2012-08-20", "count" : 2, "scaledCount" : 20 }, { "day" : "2012-08-21", "count" : 2, "scaledCount" : 20 }, { "day" : "2012-08-22", "count" : 1, "scaledCount" : 10 } ]
標(biāo)簽:邯鄲 吉安 丹東 昭通 本溪 鶴崗 景德鎮(zhèn) 大理
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB聚合功能淺析》,本文關(guān)鍵詞 MongoDB,聚合,功能,淺析,MongoDB,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。