如何使用汇总功能访问嵌入的文档 [英] How to access embeded documents using aggregation function

查看:54
本文介绍了如何使用汇总功能访问嵌入的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

db.logdata.findOne()
{
    "_id" : ObjectId("552cfc949258ff1fa8686f1a"),

    "ldl_date" : "2015-04-09",

    "ldl_mmo_id" : 5,

    "ldl_master_info_id" : 11,

    "ldl_publication_info_id" : 41616,

    "detail" : [

        {
            "ldl_id" : 54261629,

            "ldl_xml_info_id" : 37437691,

            "ldl_distribution_id" : 3289,

            "ldl_local_flag" : 1,

            "ldl_ftp_flag" : 0,

            "ldl_time" : "2015-04-09 06:36:46"
        }

    ]

}

我需要访问ldl_local_flag和我尝试执行以下查询的ldl_local_flag的计数,但未获得确切结果.

I need to access the ldl_local_flag and count of the ldl_local_flag I tried following query but not get the exact result.

查询是

db.logdata.aggregate([
    {
    $group: {
        _id: "$ldl_mmo_id",
        total: {
            $sum: "$detail.ldl_local_flag"
        }
    }
    },
    {
    $limit: 10
    }
])

输出为

{ "_id" : 1, "total" : 0 }

{ "_id" : 2, "total" : 0 }

{ "_id" : 3, "total" : 0 }

{ "_id" : 4, "total" : 0 }

{ "_id" : 5, "total" : 0 }

请帮助我..........

please help me..........

推荐答案

这可能对您有帮助:

    db.collection.aggregate({
  $unwind: '$detail'
},
{
  $group: {
    "_id": "$detail.ldl_local_flag",
    "sum": {
      "$sum": "$detail.ldl_local_flag"
    }
  }
},
{
  $project: {
    "local_flags": "$_id",
     "count": "$sum"
  }
})

这篇关于如何使用汇总功能访问嵌入的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆