I have 2 examples:
results = coll.group( key={"ip": 1, "id" : 1 }, condition= {}, initial={},
reduce="function(obj,prev) {}" )
print len(results)
and:
map = Code(
"function () {"
"emit({ id: this.id, ip: this.ip}, {count: 1});"
"}"
)
reduce = Code("function (key, values) {""}")
result = coll.map_reduce(map, reduce, "map_reduce_example")
print result.count()
Why second example more slowly than first ? I want to use 2 example instead of 1 example because 1 example not work for more than 20000 uniq key.
When you’re running map/reduce, your
mapandreducefunctions are executed in javascript runtime (which is slower than native C++ code). This also involves some locking (JS locks, read locks, write locks).group, on the other hand, might be executed more efficiently (more native code, less locks, etc).Note that in a sharded enviroment, map/reduce is your only option for now (in future versions you’ll be able to use Aggregation Framework).