pymongo 64位无符号整数

发布于 2024-12-26 18:10:36 字数 764 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2025-01-02 18:10:36

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

并运行此脚本:

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

打印:

crc type: <type 'int'>

并从 mongo shell:

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

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:

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

> db.foo.findOne()
{ "_id" : 1, "crc64" : NumberLong("9007199254740992") }
> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文