mongodb得到计数组

时间:2017-10-19 05:57:49

标签: mongodb aggregation-framework

我有按群数计算的问题。

"BOOL_LIST": [
        {
            "BOOK_TITLE": "the lord of the ring",
            "INDEX": 1,
            "READ": {
                "NORMAL": {
                    "SN": "12222ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "booked"
                },
                "bNORMAL": {
                    "SN": "4444454b51ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "yet"
                }
            }
        },....

我想得到如下的输出。

[{BOOL_TITLE:"the lord of the ring", NORMAL.booked_count : 2, bNORMAL.booked_count :1}, 
 {BOOL_TITLE:"Mr.porter", NORMAL.booked_count : 21, bNORMAL.booked_count :1}, ...]

我在使用$group时遇到问题。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您可以使用此脚本。

db.verification_job.aggregate( [
{ $unwind: "$BOOL_LIST" },
{ $group : {_id :  {BOOK_TITLE: "$BOOL_LIST.BOOK_TITLE"} 
       , NORMAL_booked_count : { $sum : { $cond:[  { $eq: ["$BOOL_LIST.READ.NORMAL.RESULT", "booked" ] } ,  1, 0 ]  } }
       , bNORMAL_booked_count : { $sum : { $cond:[ { $eq: ["$BOOL_LIST.READ.bNORMAL.RESULT", "booked" ] } , 1 ,  0 ]  } }
   }}
] )

为了更准确的测试,我假设我们有这样的样本数据。

db.verification_job.insert({
"BOOL_LIST": [
        {
            "BOOK_TITLE": "the lord of the ring",
            "INDEX": 1,
            "READ": {
                "NORMAL": {
                    "SN": "12222ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "booked"
                },
                "bNORMAL": {
                    "SN": "4444454b51ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "yet"
                }
            }
        },
        {
            "BOOK_TITLE": "the lord of the ring",
            "INDEX": 2,
            "READ": {
                "NORMAL": {
                    "SN": "12222ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "yet"
                },
                "bNORMAL": {
                    "SN": "4444454b51ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "booked"
                }
            }
        },
        {
            "BOOK_TITLE": "Mr.porter",
            "INDEX": 3,
            "READ": {
                "NORMAL": {
                    "SN": "12222ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "booked"
                },
                "bNORMAL": {
                    "SN": "4444454b51ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "yet"
                }
            }
        }
]
})

我得到了这个结果。

{ "_id" : { "BOOK_TITLE" : "Mr.porter" }, "NORMAL_booked_count" : 1, "bNORMAL_booked_count" : 0 }
{ "_id" : { "BOOK_TITLE" : "the lord of the ring" }, "NORMAL_booked_count" : 1, "bNORMAL_booked_count" : 1 }

答案 1 :(得分:0)

修改后的集合Schema,READ被修改为子文档数组而不是嵌套的子文档,但是如果您不能在模式中进行此修改,请使用$objectToArray将文档修改为数组,然后您可以继续$unwind。请注意,$ objectToArray仅适用于mongodb版本3.4.4

db.collection.aggregate([
{$unwind:"$BOOL_LIST"}, 
{$unwind:"$BOOL_LIST.READ"},
{$group:{_id:{"book_title":"$BOOL_LIST.BOOK_TITLE", 
              "result_bnormal":"$BOOL_LIST.READ.bNORMAL.RESULT", 
              "result_normal":"$BOOL_LIST.READ.NORMAL.RESULT" }, 
               count:{$sum:1}}
}])

修改后的示例文档

{"BOOL_LIST": [
        {
            "BOOK_TITLE": "the lord of the ring",
            "INDEX": 1,
            "READ": [{
                "NORMAL": {
                    "SN": "12222ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "booked"
                }},
                {"bNORMAL": {
                    "SN": "4444454b51ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "yet"
                }}
            ]
        },
    {
            "BOOK_TITLE": "The coblet of fire",
            "INDEX": 2,
            "READ": [
                {"NORMAL": {
                    "SN": "23232ea8e679518021f01a19cc4d95b9483e3",
                    "RESULT": "booked"
                }},
                {"bNORMAL": {
                    "SN": "5555554b51ea8e679518021f01a19cc4d95b9483c3",
                    "RESULT": "yet"
                }}
            ]
        }
]}

在修改后的集合中执行查询后,结果为

{
        "_id" : {
                "book_title" : "the lord of the ring",
                "result_normal" : "booked"
        },
        "count" : 1
}
{
        "_id" : {
                "book_title" : "the lord of the ring",
                "result_bnormal" : "yet"
        },
        "count" : 1
}
{
        "_id" : {
                "book_title" : "The coblet of fire",
                "result_bnormal" : "yet"
        },
        "count" : 1
}
{
        "_id" : {
                "book_title" : "The coblet of fire",
                "result_normal" : "booked"
        },
        "count" : 1
}