使用PyMongo在MongoDB上进行聚合查询

时间:2015-11-25 10:09:35

标签: python mongodb aggregation-framework pymongo

我正在编写Python脚本以使用PyMongo连接到MongoDB并运行聚合查询。

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
}

输出得到:

import datetime, time
import sys
import traceback
from pymongo import MongoClient

client = MongoClient(IP, PortNo))
db = client[Dbname]
print db

startdate = datetime.datetime.strptime(from_time,'%Y-%m-%d %H:%M:%S')
enddate = datetime.datetime.strptime(to_time,'%Y-%m-%d %H:%M:%S')

pipe = [{'$match':{"audit_time": {"$gt": startdate,"$lte": enddate}}},{'$group': {'_id':{'day': { '$dateToString': { 'format': "%Y-%m-%d", 'date': "$audit_time" } }, 'policy_id': '$policy_ids'},'SID': {'$sum': 1}}}]

result = db.compliance_name.aggregate(pipeline=pipe)
print result

在Mongo Shell上,相同的聚合查询给出了预期的输出

 Database(MongoClient('IP', 27017), u'Dbname')
 {u'ok': 1.0, u'result': [{u'SID': 1462, u'_id': {u'policy_id': [u'384509a-9ccc-4305-8678-b80b1d5795c'], u'day': u'2015-11-24'}}], u'$gleStats': {u'lastOpTime': Timestamp(0, 0), u'electionId': ObjectId('5651c922a4feda96d2e4a939')}}

为什么我们会获得额外的字段,比如Ok?从PyMongo运行时的结果和glestats。如何避免这些?

0 个答案:

没有答案