Skip to content

Unexpected behavior with & and multiple __in queries on the same field #227

@K900

Description

@K900

Hi! I'm building a long query by &-ing multiple smaller ones. It looks like when multiple __in queries on one field are combined with &, the resulting query combines all __in parameters into a single list, which produces incorrect results.

For example, running SomeModel.objects.filter(Q(some_id__in=[1]) & Q(some_id__in=[2])) should return nothing (and works correctly in upstream Django with an SQL database), but returns two objects with some_id's 1 and 2 in Django-Nonrel with MongoDB on the backend.

The SQL query is correct here: SELECT some_id, some_other_columns FROM some_table WHERE (some_table.some_id IN (1) AND some_table.some_id IN (2)).

The Mongo query, however, is (as logged by Mongo itself): "query" : {"find" : "some_collection", "filter" : {"some_id" : {"$in" : [1, 2]}}, "ntoreturn" : 21}.

Using Python 2.7.12, MongoDB 3.2.7, pip freeze:
Django==1.6.11
django-mongodb-engine==0.6.0
djangotoolbox==1.8.0
pymongo==2.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions