PyMongo组由多个键组成

时间:2015-06-28 09:27:21

标签: python mongodb mongodb-query aggregation-framework pymongo

使用PyMongo,按一个键分组似乎没问题:

results = collection.group(key={"scan_status":0}, condition={'date': {'$gte': startdate}}, initial={"count": 0}, reduce=reducer)

结果:

{u'count': 215339.0, u'scan_status': u'PENDING'} {u'count': 617263.0, u'scan_status': u'DONE'}

但是当我尝试按多个键进行分组时,我得到一个例外:

results = collection.group(key={"scan_status":0,"date":0}, condition={'date': {'$gte': startdate}}, initial={"count": 0}, reduce=reducer)

如何正确地按多个字段进行分组?

1 个答案:

答案 0 :(得分:3)

如果您尝试计算两个键,那么虽然可以使用.group(),但更好的选择是通过.aggregate()

这使用“本机代码运算符”而不是.group()所需的JavaScript解释代码来执行与您尝试实现的相同的基本“分组”操作。

特别是这里是$group管道运算符:

result = collection.aggregate([
    # Matchn the documents possible
    { "$match": { "date": { "$gte": startdate } } },

    # Group the documents and "count" via $sum on the values
    { "$group": {
        "_id": {
            "scan_status": "$scan_status",
            "date": "$date"
        },
        "count": { "$sum": 1 }
    }}
])

事实上,您可能想要将“日期”缩短为不同时期的内容。如:

result = collection.aggregate([
    # Matchn the documents possible
    { "$match": { "date": { "$gte": startdate } } },

    # Group the documents and "count" via $sum on the values
    { "$group": {
        "_id": {
            "scan_status": "$scan_status",
            "date": {
                "year": { "$year": "$date" },
                "month": { "$month" "$date" },
                "day": { "$dayOfMonth": "$date" }
            }
        },
        "count": { "$sum": 1 }
    }}
])

使用此处显示的Date Aggregation Operators

或许还有基本的“日期数学”:

import datetime
from datetime import date

result = collection.aggregate([
    # Matchn the documents possible
    { "$match": { "date": { "$gte": startdate } } },

    # Group the documents and "count" via $sum on the values
    # use "epoch" "1970-01-01" as a base to convert to integer
    { "$group": {
        "_id": {
            "scan_status": "$scan_status",
            "date": {
                "$subtract": [
                    { "$subtract": [ "$date", date.fromtimestamp(0) ] },
                    { "$mod": [
                        { "$subtract": [ "$date", date.fromtimestamp(0) ] },
                        1000 * 60 * 60 * 24
                    ]}
                ]
            }
        },
        "count": { "$sum": 1 }
    }}
])

这将从“epoch”时间返回整数值,而不是compisite值对象。

但所有这些选项都优于.group(),因为它们使用本机编码例程并以比您需要提供的JavaScript代码更快的速度执行其操作。

相关问题