php - 通过 phpDriver 插入时 Mongodb 2.6 中的 NumberLong

标签 php mongodb

当您在 Mongo 2.6 db.test.insert({a : 1, b : [2, 3]}) 中执行类似操作时,您将获得 { "_id": ObjectId("..."), "a": 3, "b": [2, 3]}。没有什么意外。

当我通过 Mongo 2.4.10 中的 php 和 1.4.5 驱动程序 进行类似操作时:

$test->insert([
 'a' => 1,
 'b' => [2 ,3]
])

我仍然得到相同的正常数字。但是当我在 Mongo 2.6.0 中做这样的事情时,结果是不同的:

{
    "_id" : ObjectId("534a...567"),
    "a" : NumberLong(1),
    "b" : [
        NumberLong(2),
        NumberLong(3)
    ]
}

如您所见,数字已转换为 NumberLong。这也是同一个整数(只是它可以更大),我不想要这种行为,因为 a)在 shell 中读取的时间更长,b)我所有的数字都低于 100000 因此那里没有 numberLong 的意义。

我正在使用 php 5.5.10mongoDriver 1.5.1

最佳答案

这是我对此的调查:

MongoShell 默认使用 32 位数字,因此我在控制台中看到这些正常数字。以前默认情况下,使用 phpDriver 插入的所有值都是 32 位的

我假设这在驱动程序中已更改,并且默认情况下现在它假设值是 64 位。可以通过 MongoInt32() 手动执行此操作来恢复正常行为。

$test->insert([
    'a' => new MongoInt32(1),
    'b' => [new MongoInt32(2), new MongoInt32(3)]
]);

这会将所有内容作为正确的短数字保存在 shell 中。仍在寻找更好的解决方案。

实际上仔细查看我的phpinfo(),我发现它有以下行mongo.native_long,值为1。实际上,这迫使驱动程序将所有内容保存为 MongoInt64 .并查看 mongo configuration 中的文档:

The default behavior for this has been changed to TRUE in 1.5.0, so make sure to set this variable to the value you want (probably TRUE) so that the driver's behavior doesn't suddenly change when you upgrade.

所以实际上这是在 1.5.0 中更改的,要重新设置它,我只需将其更改为 FALSE

为此,请转到您的 php.inimongo.ini 并添加/更改行 mongo.native_long = 0

关于php - 通过 phpDriver 插入时 Mongodb 2.6 中的 NumberLong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040905/

相关文章:

java - 将 RequestBody json 转换为对象 - Spring Boot

php - 跨多个表维护唯一标识

MyISAM > InnoDB 上的 PHP 语法错误 : expecting '}'

c# - MongoDB + C# 驱动程序 + 查询元素数组,其中每个数组元素包含要查询的子文档

javascript - 是否可以使用 MongoDB 作为 html 项目的数据库?

mongodb - Mongodb-3.4 的最佳开源 gui 工具

javascript - div 中的文本水平和垂直居中

php - 子表达式(正则表达式)的无限匹配仅返回一个匹配项

php - 已加载 Mongodb php 扩展但找不到类

mongodb - 在一个查询中查找最接近的日期