Integer problems and countermeasures when operating MongoDB in PHP

Source: Internet
Author: User

The PHP-138 mentioned in this Article "target = _ blank>IntegerIn fact, it is not a MongoDB issue, but a PHP Driver issue: MongoDB itself has two integer types: 32-bit integers and 64-bit integers, however, the old PHP driver treats all integers as 32-bit integers regardless of whether the operating system is 32-bit or 64-bit. As a result, the 64-bit integers are truncated. To solve this problem while maintaining compatibility as much as possible, the new PHP driver is addedMongo. native-longTo process all integers as 64-bit in the 64-bit operating system. For more information, see: html "target = _ blank>64-bit integers in MongoDB.

So does the PHP driver completely solve the integer problem? NO! When processing group operationsBUG:

To illustrate the problem, we first generate 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),
    ));
}

?>

Next let's use the group operation and group by group_id to calculate 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 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:

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 approach isMapReduce:

<?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 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:

 

SQL to MongoDB

In addition, there are many materials for reference, such:MongoDB Aggregation III: Map-Reduce Basics.

Note: The software version is MongoDB (1.6.5) and 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.