主頁(yè) > 知識(shí)庫(kù) > MongoDB教程之聚合(count、distinct和group)

MongoDB教程之聚合(count、distinct和group)

熱門標(biāo)簽:AI電銷 鐵路電話系統(tǒng) 呼叫中心市場(chǎng)需求 百度競(jìng)價(jià)排名 網(wǎng)站排名優(yōu)化 服務(wù)外包 Linux服務(wù)器 地方門戶網(wǎng)站

1. count:

復(fù)制代碼 代碼如下:

    --在空集合中,count返回的數(shù)量為0。
    > db.test.count()
    0
    --測(cè)試插入一個(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用來(lái)找出給定鍵的所有不同的值。使用時(shí)也必須指定集合和鍵。
 

復(fù)制代碼 代碼如下:

    --為了便于后面的測(cè)試,先清空測(cè)試集合。
    > db.test.remove()
    > db.test.count()
    0
    --插入4條測(cè)試數(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é)果文檔。
 
復(fù)制代碼 代碼如下:

    --這里是準(zhǔn)備的測(cè)試數(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
        }   
    ]


您可能感興趣的文章:
  • Mongodb聚合函數(shù)count、distinct、group如何實(shí)現(xiàn)數(shù)據(jù)聚合操作
  • MongoDB聚合group的操作指南

標(biāo)簽:衡水 湖南 黃山 銅川 蘭州 崇左 湘潭 仙桃

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB教程之聚合(count、distinct和group)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266