Blu*_*ber 1 python unsigned mongodb pymongo
我正在尝试使用pymongo在mongodb中插入一个64位无符号整数.整数是CRC64算法的输出.我试着跟随:
long(crc64(unicode(kw).encode('unicode-escape'))))
Run Code Online (Sandbox Code Playgroud)
如果我将其插入mongodb,它开始抱怨mongodb只支持64位整数.接下来我尝试将其转换为带符号的64位int,如下所示:
ctypes.c_int64(crc64(unicode(kw).encode('unicode-escape')))).value
Run Code Online (Sandbox Code Playgroud)
哪种工作,mongodb停止抱怨我的int的大小,但是当我看到mongodb中的数据时,我得到了这个:
{
"_id" : {
"floatApprox" : -5307924876159732000,
"top" : 3059119730,
"bottom" : 2651469802 },
"keyword" : "redacted",
"normal_hash" : {
"floatApprox" : -671156942315906300,
"top" : 4138701393,
"bottom" : 549001936
}
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?有没有办法将64位int作为一个int放入db(不关心它是有符号还是无符号.)
MongoDB使用BSON存储数据,BSON规范称64位整数被签名.
64位计算机上的示例会话,64位mongo v2.0.1,python 2.6.5:
>>> num = long(9007199254740992)
>>> num
9007199254740992L
>>> bson = BSON.encode({"crc64":num})
>>> bson
'\x14\x00\x00\x00\x12crc64\x00\x00\x00\x00\x00\x00\x00 \x00\x00'
>>> bson_str = bson.decode()
>>> bson_str
{u'crc64': 9007199254740992}
>>>
Run Code Online (Sandbox Code Playgroud)
并运行此脚本:
db.foo.save({"_id" : 1, "crc64" : long(9007199254740992)});
for doc in db.foo.find({"_id" : 1 }):
crc = doc["crc64"]
print("crc type: " + str(type(crc)))
Run Code Online (Sandbox Code Playgroud)
打印:
crc type: <type 'int'>
Run Code Online (Sandbox Code Playgroud)
从mongo shell:
> db.foo.findOne()
{ "_id" : 1, "crc64" : NumberLong("9007199254740992") }
>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3315 次 |
| 最近记录: |