In this paper, the PHP-138 "target=_blank>
integer Problemis not a problem with MongoDB, but a php-driven problem: MongoDB itself has two types of integers, namely: 32-bit integers and 64-bit integers, 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, As a result, 64-bit integers are truncated. In order to solve this problem with the same compatibility as possible, the new PHP driver added
Mongo.native-longoption to handle integers in 64-bit operating systems as 64-bit, interesting to refer to: HTML "target=_blank>
64-bit integers in MongoDB。
So does the PHP driver really solve the problem of integers completely? No! There are also bugswhen dealing with group operations:
To illustrate the problem, let's start by generating some test data:
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), )); }
?>
|
Let's use the group operation to summarize the count based on the group_id grouping:
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:
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:
!--? php Ini_set (Mongo.native_long, 1); br> $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/478850.html www.bkjia.com true http://www.bkjia.com/PHPjc/478850.html techarticle In this paper, the PHP-138 "target=_blank> integer problem is not a problem of MongoDB, but a php-driven problem: MongoDB itself has two types of integers, namely: 32-bit integers and 6 ...