如何使用聚合函数访问嵌入的文档

时间:2015-04-15 10:17:08

标签: mongodb mongoose mongodb-query

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的{​​{1}}和我试过以下查询的计数,但没有得到确切的结果。

查询

ldl_local_flag

输出

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

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

1 个答案:

答案 0 :(得分:0)

这可能对您有所帮助:

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