无码视频在线观看,99人妻,国产午夜视频,久久久久国产一级毛片高清版新婚

  • 始創(chuàng)于2000年 股票代碼:831685
    咨詢熱線:0371-60135900 注冊有禮 登錄
    • 掛牌上市企業(yè)
    • 60秒人工響應(yīng)
    • 99.99%連通率
    • 7*24h人工
    • 故障100倍補(bǔ)償
    全部產(chǎn)品
    您的位置: 網(wǎng)站首頁 > 幫助中心>文章內(nèi)容

    MongoDB新的數(shù)據(jù)統(tǒng)計(jì)框架介紹

    發(fā)布時(shí)間:  2012/8/31 17:35:01

    目前的MongoDB在進(jìn)行復(fù)雜的數(shù)據(jù)統(tǒng)計(jì)計(jì)算時(shí)都需要寫MapReduce來實(shí)現(xiàn),包括在SQL中比較常用的group by查詢也需要寫一個(gè)reduce才能實(shí)現(xiàn),這是比較麻煩的。在MongoDB2.1中,將會引入一套全新的數(shù)據(jù)統(tǒng)計(jì)計(jì)算框架,讓用戶更方便的進(jìn)行統(tǒng)計(jì)操作。
    -
     

    下面我們就來看看幾個(gè)新的操作符:

    $match
    $match的作用是過濾數(shù)據(jù),通過設(shè)置一個(gè)條件,將數(shù)據(jù)進(jìn)行篩選過濾,例子:

    db.runCommand({ aggregate : "article", pipeline : [
        { $match : { author : "dave" } }
    ]});這相當(dāng)于將article這個(gè)collection中的記錄進(jìn)行篩選,篩選條件是author屬性值為dave,其作用其實(shí)相當(dāng)于普通的find命令,如:

    > db.article.find({ author : "dave" });
    所以,那這個(gè)命令有什么用呢?與find不同,find的結(jié)果是直接作為最終數(shù)據(jù)返回,而$match只是pipeline中的一環(huán),它篩選的結(jié)果數(shù)據(jù)可以再進(jìn)行下一級的統(tǒng)計(jì)操作。

    $project
    $project命令用于設(shè)定數(shù)據(jù)的篩選字段,就像我們SQL中select需要的字段一樣。例子:

    db.runCommand({ aggregate : "article", pipeline : [
        { $match : { author : "dave" } },
        { $project : {
            _id : 0,
     author : 1,
            tags : 1
        }}
    ]});上面就是將所有author為dave的記錄的author和tags兩個(gè)字段取出來。(_id:0 表示去掉默認(rèn)會返回的_id字段)

    其實(shí)上面這個(gè)功能也能用我們平時(shí)用的find命令來實(shí)現(xiàn),如:

    > db.article.find({ author : "dave" }, { _id : 0, author : 1, tags : 1);
    $unwind
    $unwind命令很神奇,他可以將某一個(gè)為array類型字段的數(shù)據(jù)拆分成多條,每一條包含array中的一個(gè)屬性。
    比如你使用下面命令添加一條記錄:

    db.article.save( {
        title : "this is your title" ,
        author : "dave" ,
        posted : new Date(4121381470000) ,
        pageViews : 7 ,
        tags : [ "fun" , "nasty" ] ,
        comments : [
            { author :"barbara" , text : "this is interesting" } ,
            { author :"jenny" , text : "i like to play pinball", votes: 10 }
        ],
        other : { bar : 14 }
    });這里面tags字段就是一個(gè)array。下面我們在這個(gè)字段上應(yīng)用$unwind操作

    db.runCommand({ aggregate : "article", pipeline : [
        { $unwind : "$tags" }
    ]});上面命令的意思就是按tags字段來拆分,此命令執(zhí)行的結(jié)果如下:

    {
            "result" : [
                    {
                            "_id" : ObjectId("4eeeb5fef09a7c9170df094b"),
                            "title" : "this is your title",
                            "author" : "dave",
                            "posted" : ISODate("2100-08-08T04:11:10Z"),
                            "pageViews" : 7,
                            "tags" : "fun",
                            "comments" : [
                                    {
                                            "author" : "barbara",
                                            "text" : "this is interesting"
                                    },
                                    {
                                            "author" : "jenny",
                                            "text" : "i like to play pinball",
                                            "votes" : 10
                                    }
                            ],
                            "other" : {
                                    "bar" : 14
                            }
                    },
                    {
                            "_id" : ObjectId("4eeeb5fef09a7c9170df094b"),
                            "title" : "this is your title",
                            "author" : "dave",
                            "posted" : ISODate("2100-08-08T04:11:10Z"),
                            "pageViews" : 7,
                            "tags" : "nasty",
                            "comments" : [
                                    {
                                            "author" : "barbara",
                                            "text" : "this is interesting"
                                    },
                                    {
                                            "author" : "jenny",
                                            "text" : "i like to play pinball",
                                            "votes" : 10
                                    }
                            ],
                            "other" : {
                                    "bar" : 14
                            }
                    }
            ],
            "ok" : 1
    }我們可以看到,原來的tags字段是一個(gè)包含兩個(gè)元素的數(shù)組,通過$unwind命令后,被拆分成兩條記錄,每一條記錄的tags字段是原來數(shù)組中的一個(gè)元素。

    $group
    $group命令比較好理解,功能就是按某一個(gè)key將key值相同的多條數(shù)據(jù)組織成一條。
    比如我們使用下面命令再往article這個(gè)collection中寫入一條記錄,這時(shí)候我們就有兩條記錄了:

    db.article.save( {
        title : "this is some other title" ,
        author : "jane" ,
        posted : new Date(978239834000) ,
        pageViews : 6 ,
        tags : [ "nasty" , "filthy" ] ,
        comments : [
            { author :"will" , text : "i don't like the color" } ,
            { author :"jenny" , text : "can i get that in green?" }
        ],
        other : { bar : 14 }
    });我們可以先用上面的$unwind按tags將記錄拆成多條,然后再將記錄按tags字段重新組織,將同一個(gè)tag對應(yīng)的所有author放在一個(gè)array中。只需要像下面這樣寫:

    db.runCommand({ aggregate : "article", pipeline : [
        { $unwind : "$tags" },
        { $group : {
     _id : "$tags",
            count : { $sum : 1 },
     authors : { $addToSet : "$author" }
        }}
    ]});這時(shí)候你就能得到如下結(jié)果了

    {
            "result" : [
                    {
                            "_id" : "filthy",
                            "count" : 1,
                            "authors" : [
                                    "jane"
                            ]
                    },
                    {
                            "_id" : "fun",
                            "count" : 1,
                            "authors" : [
                                    "dave"
                            ]
                    },
                    {
                            "_id" : "nasty",
                            "count" : 2,
                            "authors" : [
                                    "jane",
                                    "dave"
                            ]
                    }
            ],
            "ok" : 1
    }上面是2.1版本將會推出的一些新的統(tǒng)計(jì)類命令的介紹,在易用性方面它們提供給我們很多便利,但是MongoDB MapReduce的最大硬傷,單個(gè)mongod中無法并行執(zhí)行,貌似還是沒有解決。雖然其命令中采用了pipeline 的組織模式,但是貌似還是完全串行且分降段完成的。


    本文出自:億恩科技【mszdt.com】

    服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經(jīng)營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經(jīng)營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經(jīng)營性ICP/ISP證:贛B2-20080012
  • 服務(wù)器/云主機(jī) 24小時(shí)售后服務(wù)電話:0371-60135900
  • 虛擬主機(jī)/智能建站 24小時(shí)售后服務(wù)電話:0371-60135900
  • 專注服務(wù)器托管17年
    掃掃關(guān)注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權(quán)所有  地址:鄭州市高新區(qū)翠竹街1號總部企業(yè)基地億恩大廈  法律顧問:河南亞太人律師事務(wù)所郝建鋒、杜慧月律師   京公網(wǎng)安備41019702002023號
      1
     
     
     
     

    0371-60135900
    7*24小時(shí)客服服務(wù)熱線