Skip to content

map_reduce method's behavior can lead to unexpected side effects and it breaks QuerySet chaining #471

@swistakm

Description

@swistakm

Now map_reduce method is messing with you. Why does it change your decisions? Why does it outputs to "inline" collection (it will be explicitely named "inline") when there is ordering on queryset instead of calculating it inline (like expected)?
If it can't sort input documents before doing inline map-reduce it should tell you that and raise an exception or at least raise a warning!
Now it sweeps problem under the carpet and you don't even know that there is any issue. Problem is that it creates some side-effects (new collection) despite it it was told to not do so (inline calculation).

More interesting fact is that you can actually sort input documents even using inline mapReduce:
http://docs.mongodb.org/manual/reference/command/mapReduce/#dbcmd.mapReduce

Moreover it doesn't even sort input documents as it (intuitively) should do. Let see what this code will do:

SomeDocument.objects.filter(key=key).order_by('some_key').map_reduce(map_code, reduce_code)

Some would think that it will make mongo find (filter) documents, sort them (order_by) and finally make map-reduce operation on them. But it actually sorts only output query of map_reduce. Mongoengine's map_reduce docs says that it supports QuerySet chaining but in my opinion this isn't a chain if some operations from front part of chain can actually be executed at the very end.

I think this is a bug or design flaw and should be fixed especially because none of behaviours descibed here are noted in documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions