当您在 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.10
和 mongoDriver 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.ini
或 mongo.ini
并添加/更改行 mongo.native_long = 0
关于php - 通过 phpDriver 插入时 Mongodb 2.6 中的 NumberLong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040905/