The integer problem mentioned in this article is not a MongoDB issue, but a PHP Driver issue.
The integer problem mentioned in this article is not a MongoDB issue, but a PHP Driver issue.
MongoDB itself has two integer types: 32-bit integer and 64-bit integer. However, the old version of PHP driver does not matter whether the operating system is 32-bit or 64-bit, all integers are treated as 32-bit integers. As a result, 64-bit integers are truncated. To solve this problem while keeping the compatibility possible, the PHP driver of the new version adds the option to treat integers as 64-bit in the 64-bit operating system, for more information, see :.
So does the PHP driver completely solve the integer problem? NO! When processing group operations, there are:
To illustrate the problem, we first generate some test data:
The Code is as follows:
Ini_set ('mongo. native_long ', 1 );
$ Instance = new Mongo ();
$ Instance = $ instance-> selectCollection ('test', 'test ');
For ($ I = 0; $ I <10; $ I ++ ){
$ Instance-> insert (array (
'Group _ id' => rand (1, 5 ),
'Count' => rand (1, 5 ),
));
}
?>
Next let's use the group operation and group by group_id to calculate the count:
The Code is as follows:
Ini_set ('mongo. native_long ', 1 );
$ Instance = new Mongo ();
$ Instance = $ instance-> selectCollection ('test', 'test ');
$ Keys = array ('group _ id' => 1 );
$ Initial = array ('Count' => 0 );
$ Reduce ='
Function (obj, prev ){
Prev. count + = obj. count;
}
';
$ Result = $ instance-> group ($ keys, $ initial, $ reduce );
Var_dump ($ result );
?>
The result is different from the Expected One. The count is not accumulated, but is changed to [object Object]. Currently, If you must use the group operation, there are two ways to alleviate this problem:
The Code is as follows:
Ini_set ('mongo. native_long ', 0 );
$ Initial = array ('Count' => (float) 0 );
Both of these methods are temporary and temporary remedies. Since the group implementation in the PHP driver is faulty, we can bypass it and implement the same function in other ways, this method is:
The Code is as follows:
Ini_set ('mongo. native_long ', 1 );
$ Instance = new Mongo ();
$ Instance = $ instance-> selectDB ('test ');
$ Map ='
Function (){
Emit (this. group_id, this. count );
}
';
$ Reduce ='
Function (key, values ){
Var sum = 0;
For (var index in values ){
Sum + = values [index];
}
Return sum;
}
';
$ Result = $ instance-> command (array (
'Mapreduce' => 'test ',
'Map' => $ map,
'Reduce' => $ reduce
));
$ Result = iterator_to_array ($ instance-> {$ result ['result']}-> find ());
Var_dump ($ result );
?>
It takes three steps to put the elephant in the refrigerator, while using MapReduce requires only two steps: Map and Reduce, here is a PDF file that vividly illustrates the relationship between group by in MySQL and MapReduce in MongoDB:
In addition, there are many materials for reference, such :.
Note: The software version is MongoDB (1.6.5) and PECL Mongo (1.1.4 ). Different versions may have different conclusions.