how to count the total number of documents after pymongo aggregation query?
related codes
match1 = {"$match": {"regDate": {"$gte": datetime(2018, 6, 1), "$lt": datetime(2018, 6, 30)}}}
lookup = {"$lookup":
{
"from": "recharge",
"localField": "_id",
"foreignField": "uid",
"as": "recharge"
}
}
project = {"$project":
{
"_id": 1,
"regDate": 1,
"recharge.from": 1,
"recharge.rechargeDate": 1,
}
}
match2 = {"$match": {"recharge.from": "weixin"}}
match3 = {"$match": {"recharge.from": "alipay"}}
match4 = {"$match": {"recharge.rechargeDate": {"$gte": datetime(2018, 6, 1), "$lt": datetime(2018, 6, 30)}}}
pipeline = [match1, lookup, project, match2, match3, match4]
result = collection.aggregate(pipeline)
for i in result:
pprint.pprint(i)
part of the result information is as follows:
{"_id": ObjectId("5b1359c11a6206742ffc3848"),
"recharge": [{"from": "regCoupon",
"rechargeDate": datetime.datetime(2018, 6, 3, 3, 13, 33, 591000)},
{"from": "alipay"},
{"from": "weixin",
"rechargeDate": datetime.datetime(2018, 6, 3, 3, 14, 32, 525000)}],
"regDate": datetime.datetime(2018, 6, 3, 3, 0, 17, 300000)}
{"_id": ObjectId("5b13bc0e1a6206742ffc3960"),
"recharge": [{"from": "regCoupon",
"rechargeDate": datetime.datetime(2018, 6, 3, 9, 59, 55, 503000)},
{"from": "alipay"},
{"from": "weixin",
"rechargeDate": datetime.datetime(2018, 6, 3, 14, 43, 16, 430000)},
{"from": "weixin",
"rechargeDate": datetime.datetime(2018, 6, 10, 11, 3, 18, 861000)}],
"regDate": datetime.datetime(2018, 6, 3, 9, 59, 42, 470000)}
{"_id": ObjectId("5b1419181a6206742ffc3a2d"),
"recharge": [{"from": "regCoupon",
"rechargeDate": datetime.datetime(2018, 6, 3, 16, 36, 46, 138000)},
{"from": "weixin",
"rechargeDate": datetime.datetime(2018, 6, 3, 19, 13, 30, 810000)},
{"from": "weixin",
"rechargeDate": datetime.datetime(2018, 6, 3, 20, 24, 3, 236000)},
{"from": "weixin",
"rechargeDate": datetime.datetime(2018, 6, 4, 1, 48, 56, 870000)},
{"from": "alipay"},
{"from": "weixin",
"rechargeDate": datetime.datetime(2018, 7, 10, 2, 59, 1, 594000)},
{"from": "weixin",
"rechargeDate": datetime.datetime(2018, 7, 21, 6, 33, 31, 558000)}],
"regDate": datetime.datetime(2018, 6, 3, 16, 36, 40, 934000)}