Home > Database > Mysql Tutorial > pymongo教程(2)聚合操作

pymongo教程(2)聚合操作

WBOY
Release: 2016-06-07 16:34:05
Original
1129 people have browsed it

在MongoDB中常用的聚合操作有 aggregation、map/reduce和group 。 首先先添加一些测试数据: db.things.insert({"x": 1, "tags": ["dog", "cat"]})db.things.insert({"x": 2, "tags": ["cat"]})db.things.insert({"x": 2, "tags": ["mouse", "cat", "dog"]})

在MongoDB中常用的聚合操作有 aggregation、map/reduce和group 。

首先先添加一些测试数据:

db.things.insert({"x": 1, "tags": ["dog", "cat"]})
db.things.insert({"x": 2, "tags": ["cat"]})
db.things.insert({"x": 2, "tags": ["mouse", "cat", "dog"]})
db.things.insert({"x": 3, "tags": []})
Copy after login

aggregation

以下例子是统计 tags 字段内的各个值的出现的次数。

from bson.son import SON
db.things.aggregate([
    {"$unwind": "$tags"},
    {"$group": {"_id": "$tags", "count": {"$sum": 1}}},
    {"$sort": SON([("count", -1), ("_id", -1)])}
])
{'ok': 1.0, 'result': [{'count': 3, '_id': 'cat'}, {'count': 2, '_id': 'dog'}, {'count': 1, '_id': 'mouse'}]}
Copy after login

注意:aggregate操作要求服务器程序为 2.1.0 以上的版本。PyMongo 驱动程序为 2.3 以上的版本。

Map/Reduce

上面的操作同样也可以使用 Map/Reduce 完成。

from bson.code import Code
mapper = Code("""
    function () {
      this.tags.forEach(function(z) {
        emit(z, 1);
      });
    }
""")
reducer = Code("""
    function (key, values) {
      var total = 0;
      for (var i = 0; i 
<p>map和reduce都是一个javascript的函数; map_reduce 方法会将统计结果保存到一个临时的数据集合中。</p>
<h2>Group</h2>
<p>group 操作与SQL的 GROUP BY 相似,同时比 Map/Reduce 要简单。</p>
<pre class="brush:php;toolbar:false">reducer = Code("""
    function(obj, prev){
      prev.count++;
    }
""")
results = db.things.group(key={"x":1}, condition={}, initial={"count": 0}, reduce=reducer)
for doc in results:
    print(doc)
{'x': 1.0, 'count': 1.0}
{'x': 2.0, 'count': 2.0}
{'x': 3.0, 'count': 1.0}
Copy after login

注意:在MongoDB的集群环境中不支持 group 操作,可以使用 aggregation 或者 map/reduce 代替。

完整的MongoDB聚合文档: http://docs.mongodb.org/manual/aggregation/

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template