pymongo 64位无符号整数
我正在尝试使用 pymongo 在 mongodb 中插入一个 64 位无符号整数。该整数是 CRC64 算法的输出。我尝试执行以下操作:
long(crc64(unicode(kw).encode('unicode-escape'))))
如果我将其插入 mongodb,它会开始抱怨 mongodb 仅支持 64 位整数。接下来,我尝试将其转换为有符号的 64 位 int,如下所示:
ctypes.c_int64(crc64(unicode(kw).encode('unicode-escape')))).value
哪种有效,mongodb 不再抱怨我的 int 的大小,但是当我查看 mongodb 中的数据时,我得到:
{
"_id" : {
"floatApprox" : -5307924876159732000,
"top" : 3059119730,
"bottom" : 2651469802 },
"keyword" : "redacted",
"normal_hash" : {
"floatApprox" : -671156942315906300,
"top" : 4138701393,
"bottom" : 549001936
}
}
这里发生了什么?有没有办法将 64 位 int 作为 int 放入数据库中(并不关心它是有符号还是无符号。)
I'm trying to insert a 64bit unsigned integer in mongodb using pymongo. The integer is the output of a CRC64 algorithm. 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. Next I tried to convert it to a signed 64bit int like so:
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:
{
"_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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MongoDB 使用 BSON 来存储数据,BSON 规范规定 64 位整数是有符号的。
64 位机器、64 位 mongo v2.0.1、python 2.6.5 上的示例会话:
并运行此脚本:
打印:
并从 mongo shell:
MongoDB uses BSON to store data, and the BSON spec says 64bit integer numbers are signed.
A sample session on a 64bit machine, 64bit mongo v2.0.1, python 2.6.5:
and running this script:
prints:
and from the mongo shell: