当您在Mongo 2.6 db.test.insert({a:1,b:[2,3]})中执行此类操作时,您将获得{“_ id”:ObjectId(“…”),“a”: 3,“b”:[2,3]}.没什么意外的.
当我在Mongo 2.4.10中用1.4.5驱动程序通过php做类似的时候:
$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