通过phpDriver插入Mongodb 2.6中的NumberLong [英] NumberLong in Mongodb 2.6 when inserting through phpDriver
问题描述
在Mongo 2.6 db.test.insert({a : 1, b : [2, 3]})
中执行类似的操作时,您会得到{ "_id" : ObjectId("..."), "a" : 3, "b" : [2, 3]}
.没什么意外的.
When you do something like this in Mongo 2.6 db.test.insert({a : 1, b : [2, 3]})
you will get { "_id" : ObjectId("..."), "a" : 3, "b" : [2, 3]}
. Nothing unexpected.
当我通过Mongo 2.4.10
中的Mongo 2.4.10
通过php执行类似操作时:
When I was doing similar through php in Mongo 2.4.10
with 1.4.5 driver
:
$test->insert([
'a' => 1,
'b' => [2 ,3]
])
我仍然得到相同的正常数字.但是,当我在Mongo 2.6.0
中执行类似操作时,结果将有所不同:
I was still getting the same normal numbers. But when I do something like this in Mongo 2.6.0
the result is different:
{
"_id" : ObjectId("534a...567"),
"a" : NumberLong(1),
"b" : [
NumberLong(2),
NumberLong(3)
]
}
如您所见,数字将转换为NumberLong
.同样,这是相同的整数(只能更大),我不希望出现这种情况,因为a)在外壳中读取的时间更长,b)我所有的数字都在100000
以下,因此没有意义在那边有号码.
As you see the numbers are converted to NumberLong
. Also that's the same integer (only it can be much bigger), I do not want this behavior, because a) it is longer to read in the shell, b) all my numbers are below 100000
and therefore there is no point to have numberLong there.
我将php 5.5.10
与mongoDriver 1.5.1
一起使用
推荐答案
这是我对此的调查:
MongoShell默认使用32位数字,因此我在控制台中看到了这些普通数字.以前默认情况下,用phpDriver插入的所有值都是32位
MongoShell uses 32-bit numbers by default, and therefore I see these normal numbers in the console. Previously by default all values inserted with phpDriver were 32-bit
我假设在驱动程序中已对此进行了更改,并且默认情况下现在假设值是64位.通过使用 MongoInt32()
I assume this was changed in the driver and by default right now it assumes that values are 64bits. It is possible to come back to normal behavior by doing this manually with MongoInt32()
$test->insert([
'a' => new MongoInt32(1),
'b' => [new MongoInt32(2), new MongoInt32(3)]
]);
这会将所有内容保存为正确的短数字在shell中.仍在寻找更好的解决方案.
This will save everything as correct short numbers in the shell. Still looking for a better solution.
实际上,我仔细观察了phpinfo()
,发现它具有以下行mongo.native_long
,值是1
.实际上,这迫使驱动程序将所有内容另存为 MongoInt64 .并查看 mongo配置中的文档:
Actually looking more closely into my phpinfo()
I found that it has the following line mongo.native_long
and the value is 1
. Actually this is forcing driver to save everything as MongoInt64. And looking at documentation in mongo configuration:
此默认行为已在1.5.0中更改为TRUE,因此 确保将此变量设置为所需的值(可能为TRUE) 这样当您开车时驾驶员的行为不会突然改变 升级.
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
.
So actually this was changed in 1.5.0
and to set it back I need only to change it to FALSE
.
为此,请转到您的php.ini
或mongo.ini
并添加/更改行mongo.native_long = 0
To do this go to your php.ini
or mongo.ini
and add/change the line mongo.native_long = 0
这篇关于通过phpDriver插入Mongodb 2.6中的NumberLong的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!