Querying with custom Pymongo Binary fields

255 views
Skip to first unread message

MFB

unread,
Dec 11, 2012, 5:57:25 PM12/11/12
to mongod...@googlegroups.com
So I have made a custom field object which I transform to/from mongodb using a Pymongo SON Manipulator.  It works great, but I'm wondering how querying works on the Binary field?

My custom DateTimeField takes a datetime and converts it's string representation to Binary, then stores it in MongoDB.  How then do I query?  Do I need to pass a Binary object to the query?   db.things.find( { 'timestamp' : { '$gte' : ??? } )

Bernie Hackett

unread,
Dec 11, 2012, 6:50:41 PM12/11/12
to mongod...@googlegroups.com
> My custom DateTimeField takes a datetime and converts it's string representation to Binary, then stores it in MongoDB.

Why? PyMongo can store all valid datetime.datetime values and query for them. You don't need to do this conversion.

>>> from datetime import datetime
>>> import pymongo
>>> c = pymongo.MongoClient()
>>> c.dates.dates.find_one({'mongodate': {'$gte': datetime(2012, 12, 11, 15, 49, 27)}})
{u'_id': ObjectId('50c6692e44415e5207000000'), u'string': u'Tue, 10 Dec 3061 23:58:54 +0100', u'mongodate': datetime.datetime(3061, 12, 10, 22, 58, 54), u'title': u'Example date'}

MFB

unread,
Dec 11, 2012, 7:10:32 PM12/11/12
to mongod...@googlegroups.com
Ok, I picked a bad example.  Of course, DateTimeField does nothing new and so is a waste of space.  Let's use the Pymongo docs custom type example where the custom type calculates an integer to another integer then stores it as a Binary.  How do you query that?  Give me all the docs who's custom field is less than 10?

Bernie Hackett

unread,
Dec 11, 2012, 7:27:25 PM12/11/12
to mongod...@googlegroups.com
You can query using the bson.binary.Binary type. Here's an example using a UUID (kinda contrived since PyMongo supports UUIDs natively as well):

>>> from uuid import uuid4, UUID
>>> u = uuid4()
>>> u.bytes
'z\x0e\x0f\x18\xce\x83OW\xa5\xd6\xb8\xbd\x00#\x0c\xa5'
>>>
>>> from bson.binary import Binary
>>> c.test.bin.insert({'bin': Binary(u.bytes, 128)})
ObjectId('50c7ce8ffba52232239f7f6d')
>>> c.test.bin.find({'bin': Binary('z\x0e\x0f\x18\xce\x83OW\xa5\xd6\xb8\xbd\x00#\x0c\xa5', 128)}).count()
1
>>> list(c.test.bin.find({'bin': Binary('z\x0e\x0f\x18\xce\x83OW\xa5\xd6\xb8\xbd\x00#\x0c\xa5', 128)}))
[{u'bin': Binary('z\x0e\x0f\x18\xce\x83OW\xa5\xd6\xb8\xbd\x00#\x0c\xa5', 128), u'_id': ObjectId('50c7ce8ffba52232239f7f6d')}]



--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

MFB

unread,
Dec 11, 2012, 7:31:44 PM12/11/12
to mongod...@googlegroups.com
does the Binary type suppoort lte, gte etc?  Does it get evaluated just as a string?

Bernie Hackett

unread,
Dec 11, 2012, 7:44:16 PM12/11/12
to mongod...@googlegroups.com
> does the Binary type suppoort lte, gte etc?

Yep.

> Does it get evaluated just as a string?

Nope. It looks like the server first checks the length of the data. If the lengths are the same it uses memcmp to order values.

MFB

unread,
Dec 11, 2012, 8:10:15 PM12/11/12
to mongod...@googlegroups.com
Thanks so much

Bernie Hackett

unread,
Dec 11, 2012, 8:54:07 PM12/11/12
to mongod...@googlegroups.com
You're welcome.


Reply all
Reply to author
Forward
0 new messages