I'm using Motor async client for mongoDB in current project, and I'd like to use aggregation like group by for query. Docs contain short info about group, but no examples how.
Parameters:
I don't know what exactly means initial parameter and reduce (as a javascript string?). Can you prodive an example?
Motor's group
method takes the same parameters as PyMongo's. Adapting the PyMongo group example to Motor:
from bson.code import Code
from tornado import ioloop, gen
from motor import MotorClient
reducer = Code("""
function(obj, prev){
prev.count++;
}
""")
db = MotorClient().test
@gen.coroutine
def example():
yield db.things.remove({})
yield db.things.insert_many([
{'x': 1},
{'x': 2},
{'x': 2},
{'x': 3},
])
results = yield db.things.group(key={"x": 1}, condition={},
initial={"count": 0}, reduce=reducer)
for doc in results:
print(doc)
ioloop.IOLoop.current().run_sync(example)
This prints:
{'count': 1.0, 'x': 1.0}
{'count': 2.0, 'x': 2.0}
{'count': 1.0, 'x': 3.0}