如何使用PyMongo获取MongoDB集合中集合的特定字段的总和?

时间:2018-12-27 00:11:20

标签: python-3.x mongodb pymongo

我的MongoDB包含以下数据

{
    "_id" : ObjectId("5c1b742eb1829b69963029e8"),
    "duration" : 12,
    "cost" : 450,
"tax" : 81,
"tags" : [],
"participants" : [ 
    ObjectId("5c1b6a8f348ddb15e4a8aac7"), 
    ObjectId("5c1b742eb1829b69963029e7")
],
"initiatorId" : ObjectId("5c1b6a8f348ddb15e4a8aac7"),
"context" : "coach",
"accountId" : ObjectId("5bdfe7b01cbf9460c9bb5d68"),
"status" : "over",
"webhook" : "http://d4bdc1ef.ngrok.io/api/v1/webhook_callback",
"hostId" : "5be002109a708109f862a03e",
"createdAt" : ISODate("2018-12-20T10:51:26.143Z"),
"updatedAt" : ISODate("2018-12-20T10:51:44.962Z"),
"__v" : 0,
"endedAt" : ISODate("2018-12-20T10:51:44.612Z"),
"startedAt" : ISODate("2018-12-20T10:51:32.992Z"),
"type" : "voip"
}

{
"_id" : ObjectId("5c1b7451b1829b69963029ea"),
"duration" : 1,
"cost" : 150,
"tax" : 27,
"tags" : [],
"participants" : [ 
    ObjectId("5c1b6a8f348ddb15e4a8aac7"), 
    ObjectId("5c1b7451b1829b69963029e9")
],
"initiatorId" : ObjectId("5c1b6a8f348ddb15e4a8aac7"),
"context" : "coach",
"accountId" : ObjectId("5bdfe7b01cbf9460c9bb5d68"),
"status" : "over",
"webhook" : "http://d4bdc1ef.ngrok.io/api/v1/webhook_callback",
"hostId" : "5be002109a708109f862a03e",
"createdAt" : ISODate("2018-12-20T10:52:01.560Z"),
"updatedAt" : ISODate("2018-12-20T10:52:08.018Z"),
"__v" : 0,
"endedAt" : ISODate("2018-12-20T10:52:07.667Z"),
"startedAt" : ISODate("2018-12-20T10:52:06.762Z"),
"type" : "voip"
}

我想获取特定帐户ID的总持续时间(持续时间字段的总和),其中在特定日期范围内状态等于“结束”。无论如何要使用PyMongo完成此操作?我无法形成查询

1 个答案:

答案 0 :(得分:0)

在将查询转换为PyMongo聚合函数时,我犯了一些非常基本的错误。我要说的是对查询结构格式要特别小心,尤其是键要封装在quotes("")中。要解决这个问题,我要做的就是

from bson.objectid import ObjectId
    pipe = [
        {"$match": {"accountId": ObjectId(accountId),
                    "status": "over",
                    "startedAt": {"$gte": startDate,
                                  "$lte": EndDate
                                  }
                    }},
        {"$project": {"readableDate":
                      {"$dateToString":

                       {"format": "%Y-%m-%d", "date": "$startedAt"}},
                      "accountId": str("$accountId"),
                      "duration": "$duration"
                      }},
        {"$group": {"_id": {"date": "$readableDate",
                            "accountId": str("$accountId")}, "totalCallDuration": {"$sum": "$duration"}}}]
    for doc in db.VoiceCall.aggregate(pipe):
        print(doc)

提醒一下:startDateEndDatePython datetime format中。