Querying a list of strings inside a DictField

825 views
Skip to first unread message

Alex Hung

unread,
Jan 27, 2014, 11:34:33 AM1/27/14
to mongoeng...@googlegroups.com
Hi,

I'm trying to query my document which has a DictField (data) containing a list of string in the field value, like this:

{
    "_cls" : "Record.ReviewRecord",
    "_id" : ObjectId("512da2e18db94274236596f0"),
    "_types" : [ 
        "Record", 
        "Record.ReviewRecord"
    ],
    "application_id" : ObjectId("512d9f1221b8ab624284973f"),
    "data" : {
        "512d9d2121b8ab6242849727" : [ 
            "512d9d2121b8ab624284972b"
        ],
        "512d9d2121b8ab6242849732" : [ 
            "512d9d2121b8ab6242849734"
        ]
    },
}

In MongoDb I get 2 results as expected using this:

db.record.find({"wizard_id": ObjectId("512d9d2021b8ab6242849721"), "data.512d9d2121b8ab6242849727": '512d9d2121b8ab624284972b'})

In Python, I use this which I thought would produce the same query and thus result:

Record.objects.filter(wizard_id=ObjectId('512d9d2021b8ab6242849721'), data__512d9d2121b8ab6242849727=['512d9d2121b8ab624284972b'])

Instead I get empty array back.

My initial thought was that the way I construct the query keyword for the data dictfield isn't working so I use the __raw__:

Record.objects.filter(__raw__={'wizard_id': ObjectId('512d9d2021b8ab6242849721'), 'data.512d9d2121b8ab6242849727': '512d9d2121b8ab624284972b'})

This also returns empty array as result. So I decided to try the query as pymongo query:

collection = ApplicationRecord._get_collection()
collection.find({'wizard_id': ObjectId('512d9d2021b8ab6242849721'), 'data.512d9d2121b8ab6242849727': '512d9d2121b8ab624284972b'})

This times I get the 2 records back in the result!

So what's going on here? Am I not constructing the query correctly in python using mongoengine API? Is this a limitation of the lib at the moment?

Thanks

Alex Hung

unread,
Jan 27, 2014, 11:35:42 AM1/27/14
to mongoeng...@googlegroups.com
I forgot to mention I'm using monoengine 0.8.7, MongoDb 2.4.6

Alex Hung

unread,
Jan 27, 2014, 11:37:37 AM1/27/14
to mongoeng...@googlegroups.com
Arggh, just spotted an error in my post.

The mongoengine query should be:

Record.objects.filter(wizard_id=ObjectId('512d9d2021b8ab6242849721'), data__512d9d2121b8ab6242849727='512d9d2121b8ab624284972b')

I've also tried with same empty result:

Record.objects.filter(wizard_id=ObjectId('512d9d2021b8ab6242849721'), data__512d9d2121b8ab6242849727__in=['512d9d2121b8ab624284972b'])

Ross Lawley

unread,
Jan 28, 2014, 4:18:55 AM1/28/14
to mongoeng...@googlegroups.com
Hi Alex,

That doesn't sound good - you can see the query that mongoengine uses by calling ._query on a QuerySet and that should shine a light on the cause: eg:

Record.objects.filter(wizard_id=ObjectId('512d9d2021b8ab6242849721'), data__512d9d2121b8ab6242849727__in=['512d9d2121b8ab624284972b'])._query

Cheers,

Ross

--
You received this message because you are subscribed to the Google Groups "MongoEngine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoengine-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages