MongoDB itself has two integer types: 32-bit integers and 64-bit integers, but older PHP drivers, regardless of whether the operating system is 32-bit or 64-bit, treat all integers as 32-bit integers, resulting in a 64-bit integer being truncated. In order to solve this problem with the same compatibility as possible, the new PHP driver added the Mongo.native-long option to handle integers as 64 bits in 64-bit operating systems, with an interest to refer to: 64-bit integers in MongoDB.
So does the PHP driver really solve the problem of integers completely? No! There are also bugs when dealing with group operations:
To illustrate the problem, let's start by generating some test data:
Copy CodeThe code is as follows:
Ini_set (' Mongo.native_long ', 1);
$instance = new Mongo ();
$instance = $instance->selectcollection (' Test ', ' test ');
for ($i = 0; $i < $i + +) {
$instance->insert (Array (
' group_id ' = rand (1, 5),
' Count ' = rand (1, 5),
));
}
?>
Let's use the group operation to summarize the count based on the group_id grouping:
Copy CodeThe 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 not the same as expected, count does not implement accumulation, but instead becomes an [object object], and currently, if you must use group operations, there are two ways to mitigate this problem:
Copy CodeThe code is as follows:
Ini_set (' Mongo.native_long ', 0);
$initial = Array (' count ' = = (float) 0);
Both of these methods are a temporary solution to the problem, since the current PHP driver implementation of the group is problematic, then we bypass it, in other ways to achieve the same function, this way is MapReduce:
Copy CodeThe 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 an elephant in the freezer, and using mapreduce requires only a map and reduce two steps, and here's a PDF document that vividly illustrates the correspondence between group by and MongoDB in MySQL:
SQL to MongoDB
In addition, there is a lot of information for reference, such as: MongoDB Aggregation iii:map-reduce Basics.
Description: The software version is MongoDB (1.6.5), PECL Mongo (1.1.4). Different versions may have different conclusions.
http://www.bkjia.com/PHPjc/323216.html www.bkjia.com true http://www.bkjia.com/PHPjc/323216.html techarticle MongoDB itself has two types of integers, namely: 32-bit integer and 64-bit integer, but the old version of PHP driver regardless of the operating system is 32-bit or 64-bit, all integers as a 32-bit integer processing, knot ...