Reputation: 1949
I can't find much of an indication in neither the pymongo documentation or the mongodb documentation. I'm currently working with python3, and I have one program inserting data and another program that queries it.
Basically, if one program inserts data as a BSON long type (64 bit), can another program query that data with a 32 bit int?
For example, where I write data.
from bson.int64 import Int64 ... coll.insert({'myval': Int64(100)})
And where I read data.
coll.find({'myval': int(100)})
Does this work regardless of how big the int is? Even ignoring if python3 stores the int as a 64 bit?
Upvotes: 1
Views: 5298
Reputation: 1372
You don't have to manually cast your number to bson.int64 on writing; pymongo will take care of this automatically.
Yes, you can read data with:
coll.find({'myval': int(100)})
or simpler:
coll.find({'myval': 100})
Upvotes: 2
Reputation: 8909
The distinction between int64 and int32 in MongoDB is more about BSON storage size. You can query with either type:
Python 3.3.5 (default, Jan 29 2015, 02:00:04)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> c = pymongo.MongoClient()
>>> from bson.int64 import Int64
>>> c.test.int64.insert_one({'int64': Int64(2 ** 32)})
<pymongo.results.InsertOneResult object at 0x7f6ece3f43c0>
>>> c.test.int64.find_one({'int64': int(2 ** 32)})
{'int64': 4294967296, '_id': ObjectId('554fa762fa5bd8782e1698ce')}
Upvotes: 2