pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <legend id='FVo5d'><style id='FVo5d'><dir id='FVo5d'><q id='FVo5d'></q></dir></style></legend>
    1. <i id='FVo5d'><tr id='FVo5d'><dt id='FVo5d'><q id='FVo5d'><span id='FVo5d'><b id='FVo5d'><form id='FVo5d'><ins id='FVo5d'></ins><ul id='FVo5d'></ul><sub id='FVo5d'></sub></form><legend id='FVo5d'></legend><bdo id='FVo5d'><pre id='FVo5d'><center id='FVo5d'></center></pre></bdo></b><th id='FVo5d'></th></span></q></dt></tr></i><div class="0quiook" id='FVo5d'><tfoot id='FVo5d'></tfoot><dl id='FVo5d'><fieldset id='FVo5d'></fieldset></dl></div>

      <small id='FVo5d'></small><noframes id='FVo5d'>

    2. <tfoot id='FVo5d'></tfoot>
      • <bdo id='FVo5d'></bdo><ul id='FVo5d'></ul>

    3. MongoDB匯總子文檔上的每個鍵

      MongoDB Aggregate Sum Each Key on a Subdocument(MongoDB匯總子文檔上的每個鍵)

      1. <tfoot id='wF8e7'></tfoot>
        1. <legend id='wF8e7'><style id='wF8e7'><dir id='wF8e7'><q id='wF8e7'></q></dir></style></legend>
          • <small id='wF8e7'></small><noframes id='wF8e7'>

              <i id='wF8e7'><tr id='wF8e7'><dt id='wF8e7'><q id='wF8e7'><span id='wF8e7'><b id='wF8e7'><form id='wF8e7'><ins id='wF8e7'></ins><ul id='wF8e7'></ul><sub id='wF8e7'></sub></form><legend id='wF8e7'></legend><bdo id='wF8e7'><pre id='wF8e7'><center id='wF8e7'></center></pre></bdo></b><th id='wF8e7'></th></span></q></dt></tr></i><div class="2kwwc0i" id='wF8e7'><tfoot id='wF8e7'></tfoot><dl id='wF8e7'><fieldset id='wF8e7'></fieldset></dl></div>
              • <bdo id='wF8e7'></bdo><ul id='wF8e7'></ul>
                  <tbody id='wF8e7'></tbody>
                本文介紹了MongoDB匯總子文檔上的每個鍵的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我有多個具有此架構(gòu)的文檔,每個文檔每天針對每個產(chǎn)品:

                I have multiple documents with this schema, each document is per product per day:

                {
                    _id:{},
                    app_id:'DHJFK67JDSJjdasj909',
                    date:'2014-08-07',
                    event_count:32423,
                    event_count_per_type: {
                        0:322,
                        10:4234,
                        20:653,
                        30:7562
                    }
                }
                

                我想獲取特定日期范圍內(nèi)每個 event_type 的總和.
                這是我正在尋找的輸出,其中每種事件類型已在所有文檔中求和.event_count_per_type 的鍵可以是任何東西,所以我需要一些可以循環(huán)遍歷它們的東西,而不必隱含它們的名稱.

                I would like to get the sum of each event_type for a particular date_range.
                This is the output I am looking for where each event type has been summed across all the documents. The keys for event_count_per_type can be anything, so I need something that can loop through each of them as opposed to be having to be implicit with their names.

                {
                    app_id:'DHJFK67JDSJjdasj909',
                    event_count:324236456,
                    event_count_per_type: {
                        0:34234222,
                        10:242354,
                        20:456476,
                        30:56756
                    }
                }
                

                到目前為止,我已經(jīng)嘗試了幾個查詢,這是迄今為止我得到的最好的查詢,但是子文檔值沒有相加:

                I have been trying several queries so far, this is the best I have got so far but the sub document values are not summed:

                db.events.aggregate(
                {
                    $match: {app_id:'DHJFK67JDSJjdasj909'}
                },
                {
                    $group: {
                        _id: {
                            app_id:'$app_id',
                        },
                        event_count: {$sum:'$event_count'},
                        event_count_per_type: {$sum:'$event_count_per_type'}
                    }
                },
                {
                    $project: {
                        _id:0,
                        app_id:'$_id.app_id',
                        event_count:1,
                        event_count_per_type:1
                    }
                }
                )
                

                我看到的輸出是 event_count_per_type 鍵的值 0,而不是對象.我可以修改架構(gòu),使鍵位于文檔的頂層,但這仍然意味著我需要在每個鍵的組語句中都有一個條目,因為我不知道鍵名是什么,所以我不能做.

                The output I am seeing is a value of 0 for the event_count_per_type key, instead of an object. I could modify the schema so the keys are on the top level of the document but that will still mean that I need to have an entry in the group statement for each key, which as I do not know what the key names will be I cannot do.

                如有任何幫助,我將不勝感激,如果需要,我愿意更改我的架構(gòu)并嘗試使用 mapReduce(盡管從文檔看來性能很差.)

                Any help would be appreciated, I am willing to change my schema if need be and also to try mapReduce (although from the documentation it seems like the performance is bad.)

                推薦答案

                如上所述,使用聚合框架處理這樣的文檔是不可能的,除非您實際上要提供所有鍵,例如:

                As stated, processing documents like this is not possible with the aggregation framework unless you are actually going to supply all of the keys, such as:

                db.events.aggregate([
                   { "$group": {
                       "_id": "$app_id",
                       "event_count": { "$sum": "$event_count" },
                       "0": { "$sum": "$event_count_per_type.0" },
                       "10": { "$sum": "$event_count_per_type.10" }
                       "20": { "$sum": "$event_count_per_type.20" }
                       "30": { "$sum": "$event_count_per_type.30" }
                   }}
                ])
                

                但您當(dāng)然必須明確指定您希望處理的每個鍵.MongoDB 中的聚合框架和一般查詢操作都是如此,因為要訪問以這種子文檔"形式標(biāo)注的元素,您需要指定元素的確切路徑"才能對其進(jìn)行任何操作.

                But you do of course have to explicitly specify every key you wish to work on. This is true of both the aggregation framework and general query operations in MongoDB, as to access elements notated in this "sub-document" form you need to specify the "exact path" to the element in order to do anything with it.

                聚合框架和通用查詢沒有遍歷"的概念,這意味著它們無法處理文檔的每個鍵".這需要一種語言結(jié)構(gòu)才能完成這些接口中未提供的功能.

                The aggregation framework and general queries have no concept of "traversal", which mean they cannot process "each key" of a document. That requires a language construct in order to do which is not provided in these interfaces.

                一般來說,使用鍵名"作為其名稱實際上代表值"的數(shù)據(jù)點有點反模式".對此進(jìn)行建模的更好方法是使用數(shù)組并將您的類型"本身表示為值:

                Generally speaking though, using a "key name" as a data point where it's name actually represents a "value" is a bit of an "anti-pattern". A better way to model this would be to use an array and represent your "type" as a value by itself:

                {
                    "app_id": "DHJFK67JDSJjdasj909",
                    "date: ISODate("2014-08-07T00:00:00.000Z"),
                    "event_count": 32423,
                    "events": [
                        { "type": 0,  "value": 322  },
                        { "type": 10, "value": 4234 },
                        { "type": 20, "value": 653  },
                        { "type": 30, "value": 7562 }
                    ]
                }
                

                還要注意日期"現(xiàn)在是一個正確的日期對象而不是一個字符串,這也是一個很好的做法.這種數(shù)據(jù)雖然很容易使用聚合框架進(jìn)行處理:

                Also noting that the "date" is now a proper date object rather than a string, which is also something that is good practice to do. This sort of data though is easy to process with the aggregation framework:

                db.events.aggregate([
                    { "$unwind": "$events" },
                    { "$group": {
                        "_id": { 
                            "app_id": "$app_id",
                            "type": "$events.type"
                        },
                        "event_count": { "$sum": "$event_count" },
                        "value": { "$sum": "$value" }
                    }},
                    { "$group": {
                        "_id": "$_id.app_id",
                        "event_count": { "$sum": "$event_count" },
                        "events": { "$push": { "type": "$_id.type", "value": "$value" } }
                    }}
                ]) 
                

                這顯示了一個兩階段分組,首先獲取每個類型"的總數(shù)而不指定每個鍵",因為您不再需要指定每個鍵",然后作為每個app_id"的單個文檔返回,結(jié)果在數(shù)組中原樣原來存儲的.這種數(shù)據(jù)形式對于查看某些類型"甚至某個范圍內(nèi)的值"通常要靈活得多.

                That shows a two stage grouping that first gets the totals per "type" without specifying each "key" since you no longer have to, then returns as a single document per "app_id" with the results in an array as they were originally stored. This data form is generally much more flexible for looking at certain "types" or even the "values" within a certain range.

                如果您無法更改結(jié)構(gòu),那么您唯一的選擇是 mapReduce.這允許您編碼"鍵的遍歷,但由于這需要 JavaScript 解釋和執(zhí)行,它不如聚合框架快:

                Where you cannot change the structure then your only option is mapReduce. This allows you to "code" the traversal of the keys, but since this requires JavaScript interpretation and execution it is not as fast as the aggregation framework:

                db.events.mapReduce(
                    function() {
                        emit(
                            this.app_id,
                            {
                                "event_count": this.event_count,
                                "event_count_per_type": this.event_count_per_type
                            }
                        );
                    },
                    function(key,values) {
                
                        var reduced = { "event_count": 0, "event_count_per_type": {} };
                
                        values.forEach(function(value) {
                            for ( var k in value.event_count_per_type ) {
                                if ( !redcuced.event_count_per_type.hasOwnProperty(k) )
                                    reduced.event_count_per_type[k] = 0;
                                reduced.event_count_per_type += value.event_count_per_type;
                            }
                            reduced.event_count += value.event_count;
                        })
                    },
                    {
                        "out": { "inline": 1 }
                    }
                )
                

                這實際上將遍歷并組合鍵",并對找到的每個鍵的值求和.

                That will essentially traverse and combine the "keys" and sum up the values for each one found.

                所以你的選擇是:

                1. 更改結(jié)構(gòu)并使用標(biāo)準(zhǔn)查詢和聚合.
                2. 保持結(jié)構(gòu)不變,需要 JavaScript 處理和 mapReduce.

                這取決于您的實際需求,但在大多數(shù)情況下,重組會產(chǎn)生好處.

                It depends on your actual needs, but in most cases restructuring yields benefits.

                這篇關(guān)于MongoDB匯總子文檔上的每個鍵的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Use IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
                anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 項目中不起作用)
                Ionic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用異步數(shù)據(jù)更新 Observable)
                Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
                In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何創(chuàng)建使用 Ionic 組件的自定義指令?)
                Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(將 ViewChild 用于動態(tài)元素 - Angular 2 amp;離子2)
                    <bdo id='453zU'></bdo><ul id='453zU'></ul>

                        <tfoot id='453zU'></tfoot>
                        • <small id='453zU'></small><noframes id='453zU'>

                        • <legend id='453zU'><style id='453zU'><dir id='453zU'><q id='453zU'></q></dir></style></legend>
                        • <i id='453zU'><tr id='453zU'><dt id='453zU'><q id='453zU'><span id='453zU'><b id='453zU'><form id='453zU'><ins id='453zU'></ins><ul id='453zU'></ul><sub id='453zU'></sub></form><legend id='453zU'></legend><bdo id='453zU'><pre id='453zU'><center id='453zU'></center></pre></bdo></b><th id='453zU'></th></span></q></dt></tr></i><div class="micmaui" id='453zU'><tfoot id='453zU'></tfoot><dl id='453zU'><fieldset id='453zU'></fieldset></dl></div>
                            <tbody id='453zU'></tbody>
                          主站蜘蛛池模板: 深圳货架厂_仓库货架公司_重型仓储货架_线棒货架批发-深圳市诺普泰仓储设备有限公司 | 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | 广东护栏厂家-广州护栏网厂家-广东省安麦斯交通设施有限公司 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 选矿设备-新型重选设备-金属矿尾矿重选-青州冠诚重工机械有限公司 | 污水提升器,污水提升泵,污水提升装置-德国泽德(zehnder)水泵系统有限公司 | 样品瓶(色谱样品瓶)百科-浙江哈迈科技有限公司 | 雨燕360体育免费直播_雨燕360免费NBA直播_NBA篮球高清直播无插件-雨燕360体育直播 | 冷却塔降噪隔音_冷却塔噪声治理_冷却塔噪音处理厂家-广东康明冷却塔降噪厂家 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | 污水处理设备-海普欧环保集团有限公司 | 济南展厅设计施工_数字化展厅策划设计施工公司_山东锐尚文化传播有限公司 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | 考试试题_试卷及答案_诗词单词成语 - 优易学| 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | 膜片万向弹性联轴器-冲压铸造模具「沧州昌运模具」 | 合肥白癜风医院_合肥治疗白癜风医院_合肥看白癜风医院哪家好_合肥华研白癜风医院 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易| 珠海网站建设_响应网站建设_珠海建站公司_珠海网站设计与制作_珠海网讯互联 | 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司 | 拉卡拉POS机官网 - 官方直营POS机办理|在线免费领取 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 一体化污水处理设备-一体化净水设备-「山东梦之洁水处理」 | 韦伯电梯有限公司 | 聚合氯化铝-碱式氯化铝-聚合硫酸铁-聚氯化铝铁生产厂家多少钱一吨-聚丙烯酰胺价格_河南浩博净水材料有限公司 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | pbt头梳丝_牙刷丝_尼龙毛刷丝_PP塑料纤维合成毛丝定制厂_广州明旺 | 缝纫客| 对夹式止回阀_对夹式蝶形止回阀_对夹式软密封止回阀_超薄型止回阀_不锈钢底阀-温州上炬阀门科技有限公司 | 直线模组_滚珠丝杆滑台_模组滑台厂家_万里疆科技 | 粉丝机械,粉丝烘干机,粉丝生产线-招远市远东粉丝机械有限公司 | 精准猎取科技资讯,高效阅读科技新闻_科技猎 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 |