简体   繁体   English

pymongo 64位无符号整数

[英]pymongo 64bit unsigned integer

I'm trying to insert a 64bit unsigned integer in mongodb using pymongo. 我正在尝试使用pymongo在mongodb中插入64位无符号整数。 The integer is the output of a CRC64 algorithm. 整数是CRC64算法的输出。 I tried to following: 我试图遵循:

long(crc64(unicode(kw).encode('unicode-escape'))))

If I insert this into mongodb it starts to complain that only 64bit integers are supported by mongodb. 如果我将其插入mongodb,它将开始抱怨mongodb仅支持64位整数。 Next I tried to convert it to a signed 64bit int like so: 接下来,我尝试将其转换为带符号的64位int,如下所示:

ctypes.c_int64(crc64(unicode(kw).encode('unicode-escape')))).value

Which kind of works, mongodb stops complaining about the size of my int, but when I look at the data in mongodb I get this: mongodb停止抱怨哪种类型的工作,但是当我查看mongodb中的数据时,我得到以下信息:

{
    "_id" : {
        "floatApprox" : -5307924876159732000,
        "top" : 3059119730,
        "bottom" : 2651469802 },
    "keyword" : "redacted",
    "normal_hash" : { 
        "floatApprox" : -671156942315906300,
        "top" : 4138701393,
        "bottom" : 549001936
    } 
}

What's going on here? 这里发生了什么? Is there any way to put the 64bit int into the db as just an int (don't really care whether it's signed or unsigned.) 有什么方法可以将64位int仅仅作为int放入数据库中(不必在乎它是否是带符号的。)

MongoDB uses BSON to store data, and the BSON spec says 64bit integer numbers are signed. MongoDB使用BSON来存储数据,并且BSON规范指出64位整数是带符号的。

A sample session on a 64bit machine, 64bit mongo v2.0.1, python 2.6.5: 在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}
>>> 

and running this script: 并运行此脚本:

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)))

prints: 印刷品:

crc type: <type 'int'>

and from the mongo shell: 从mongo shell中:

> db.foo.findOne()
{ "_id" : 1, "crc64" : NumberLong("9007199254740992") }
> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM