Integer Problems and Countermeasures during MongoDB operations in PHP _ PHP Tutorial

Source: Internet
Author: User
Description of integer problems and countermeasures when operating MongoDB in PHP. 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. MongoDB itself has two types of integers: 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 has been added to mongo. the native-long option is used to process integers as 64-bit in a 64-bit operating system. For more information, see 64-bit integers in MongoDB.

So does the PHP driver completely solve the integer problem? NO! When processing group operations, there are also bugs:

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

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:

SQL to MongoDB

In addition, there are many materials for reference, such as 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.

...

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.