Mongodb找不到_id太长的对象 [英] Mongodb can't find object with too long _id

查看:48
本文介绍了Mongodb找不到_id太长的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点奇怪的情况.

我在集合refs"中持久化对象,明确设置 _id.所以我有一些 ID 非常大的对象.

I persist objects in collection "refs" explicitly setting _id. So I have objects with very big id's.

db.refs.find().sort({_id: -1});
// {_id: 9200000000165761625}
// ...

但是当我尝试在 mongo shell 中查找具有最大 id 的对象时,它什么都不返回:

But when I try to find object with biggest id in mongo shell it returns nothing:

db.refs.find({_id: 9200000000165761625}); // nothing

但是!

db.refs.find({_id: 9200000000165761625}).count(); // return 1

怎么会这样?

推荐答案

JavaScript 目前只有一个数字类型 Number,它将所有值表示为 64 位浮点值.JavaScript 原生 Number 类型的最大安全整数表示为 253-1 或 9007199254740991(由常量 Number.MAX_SAFE_INTEGER).

JavaScript currently only has a single numeric type Number, which represents all values as 64-bit floating point values. The maximum safe integer representation in JavaScript's native Number type is 253-1 or 9007199254740991 (as returned by the constant Number.MAX_SAFE_INTEGER).

任何超出安全范围的整数值都无法明确表示,因此两个或多个数学值将映射到同一个 JavaScript 数字.

Any integer values beyond the safe range cannot be represented distinctly, so two or more mathematical values will map to the same JavaScript Number.

您可以在 mongo shell 中看到此效果,其值与您提供的 _id(大于安全整数大小)相邻:

You can see this effect in the mongo shell with values adjacent to your provided _id (which is larger than the safe integer size):

> 9200000000165761624
9200000000165762000

> 9200000000165761625
9200000000165762000

> 9200000000165761626
9200000000165762000

但是,这些驱动程序/客户端限制与 MongoDB 的BSON 格式中使用的底层数据类型不同用于文档.BSON 有一个 64 位整数类型,它表示所有值范围:对于 64 位整数,最多 263-1.

However, these driver/client limitations are distinct from the underlying data types used in MongoDB's BSON format for documents. BSON has a 64-bit integer type which represents the full range of values: up to 263-1 for 64-bit integers.

您的示例 _id 在 64 位整数范围内,因此您应该能够使用支持 64 位整数的驱动程序插入或更新它,但无法安全查询或在 mongo shell 或其他 JavaScript 环境中操作长值.为避免意外结果,您可能需要为这些长 _id 值使用不同的数据类型.

Your example _id is within the 64-bit integer range so you should be able to insert or update this using a driver with support for 64-bit integers, but would not be able to safely query or manipulate long values in the mongo shell or other JavaScript environments. To avoid unexpected outcomes you may want to use a different data type for these long _id values.

这篇关于Mongodb找不到_id太长的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆