利用pymong包聚合查询mongodb(aggregate())

上个星期,主要写了收入相关的接口测试;统计收入的过程中,需要用到MongoDB的聚合查询;目前做个简单的总结:

聚合操作处理数据记录,并返回计算值;MongoDB提供了3种方法执行聚合:the aggregation pipeline, the map-reduce function, 和单一目的的聚合方法(db.collection.count()db.collection.distinct())。

这里主要介绍the aggregation pipelineaggregate(pipeline, **kwargs)

1. 位置参数 pipeline,是一个数组列表list,具体的操作字段可参见官方文档stage-operators,例如:

2. 关键字参数 **kwargs,具体使用参数参考官方文档pymongo.collection.Collection.aggregate

3.返回值的类型是CommandCursor,是一个可迭代访问的类型,可转换为list类型 list(result),通过下标访问数据。

 

发表评论

邮箱地址不会被公开。 必填项已用*标注