Querying collection with ID

107 views
Skip to first unread message

Shezad Aibara

unread,
Nov 4, 2011, 7:59:50 AM11/4/11
to MongoEngine Users
I used to be able to query the collection using ID as a string as
{id:'id_of_document'} in v 0.4
After I updated to 0.5 I am not able to query like that. I am able to
query using
{'id':ObjectId('id_of_document')}
Is this a change which happened in the latest version? Is there any
other easy way to do a raw query using ID

Rozza

unread,
Jan 26, 2012, 3:08:19 AM1/26/12
to mongoeng...@googlegroups.com
Hi,

Can you give me an example of your use case as I'm not finding this to be the case with 0.5.2 or dev.

The following code passes:

        class Person(Document):
            name = StringField()
            age = IntField()

        Person.drop_collection()

        p = Person(name='Ross')
        p.save()

        self.assertEquals(Person.objects(pk=str(p.id)).count(), 1)

What am I missing?

Ross

Amorano

unread,
Jan 26, 2012, 4:43:52 PM1/26/12
to MongoEngine Users
I originally replied to this to say the same thing the OP said,
except, I realized that is how it works.

What is the problem is querying OUTSIDE of ME.

The _id's are not simply "2384892374982349872" but rather
ObjectId("098213409234902834")

So you actually need to put the ObjectId(" part in as well when
searching for raw IDs.

Inconvient sure, but I understand why. You need the RTTI since IDs can
be anything.

Cheers.
Reply all
Reply to author
Forward
0 new messages