Bugs and strategies for PHP-driven MongoDB integer problems

Source: Internet
Author: User
Tags array count integer mongodb prev rand reference

The integer problem in this article is not a mongodb problem, but a php-driven problem: The MongoDB itself has two types of integers: 32-bit integers and 64-bit integers, but older PHP drivers, regardless of the operating system's 32-bit or 64-bit, All integers are treated as 32-bit integers, resulting in 64-bit integers being truncated. In order to solve this problem as much as possible, the new PHP driver added the mongo.native-long option, with a view to handling integers as 64 bits in 64-bit operating systems, and interested in reference:64-bit Integers in MongoDB.

So does the PHP driver really solve the integer problem completely? No! There are also bugswhen working with group operations:

To illustrate the problem, let's start by generating some test data:

<?php

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 action, grouped by group_id, to summarize the count:

<?php

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 results are not the same as expected, and the count does not accumulate, but instead becomes [object], and now 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 stopgap measures, since the current PHP driver in the implementation of the group is problematic, then we go around it, in other ways to achieve the same function, this way is MapReduce:

<?php

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 fridge, and using mapreduce requires only map and reduce two steps, and here's a PDF document that graphically illustrates the correspondence between group by and MongoDB in MySQL:

SQL to MongoDB

In addition, there are a lot of information available 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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.