In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what is the problem when PHP operates MongoDB". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
MongoDB itself has two types of integers: 32-bit integers and 64-bit integers, but old PHP drivers treated all integers as 32-bit integers regardless of whether the operating system was 32-bit or 64-bit, resulting in 64-bit integers being truncated. The following editor will explain what are the problems when PHP operates MongoDB?
What are the problems when PHP operates MongoDB?
In order to solve this problem while maintaining compatibility as much as possible, the new version of PHP driver adds the mongo.native-long option to deal with integers as 64 bits in 64-bit operating systems. If you are interested, please refer to: 64-bitintegersinMongoDB.
So does the PHP driver really solve the integer problem completely? NO! When dealing with group operations, there is also BUG:
To illustrate, let's first generate some test data:
SelectCollection ('test','test')
For ($iposit insert (array (
'group_id'= > rand (1pc5)
'count'= > rand (1pc5)
))
}
? >
Let's use the group operation to group the group_id into groups and calculate the count:
SelectCollection ('test','test')
$keys=array ('group_id'= > 1)
$initial=array ('count'= > 0)
$reduce='
Function (obj,prev) {
Prev.count+=obj.count
}
'To
$result=$instance- > group ($keys,$initial,$reduce)
Var_dump ($result)
? >
The result is not as expected. Instead of accumulating, count becomes [objectObject]. Currently, if you have to use group operation, there are two ways to alleviate this problem:
Method 1:
Ini_set ('mongo.native_long',0)
What are the problems when PHP operates MongoDB?
Method 2:
$initial=array ('count'= > (float) 0)
These two methods are both expedient solutions to the symptoms rather than the root causes. Since there is a problem with the implementation of group in the current PHP driver, let's bypass it and achieve the same function in other ways. This way is MapReduce:
SelectDB ('test')
$map='
Function () {
Emit (this.group_id,this.count)
}
'To
$reduce='
Function (key,values) {
Varsum=0
For (varindexinvalues) {
Sum+=values [index]
}
Returnsum
}
'To
$result=$instance- > command (array (
'mapreduce'= >' test'
'out'= >' name'
'map'= > $map
'reduce'= > $reduce
))
$result=iterator_to_array ($instance- > {$result ['result']}-> find ())
Var_dump ($result)
? >
Description: although on the surface, it seems inefficient for MapReduce to generate a new Collection, we can pre-generate it regularly, which is equivalent to maintaining a cache table, as long as the real-time requirements are not too high.
It takes three steps to put an elephant in the refrigerator, but it only takes two steps to use MapReduce: Map and Reduce. Here is a PDF document that vividly illustrates the correspondence between GROUPBY in MySQL and MapReduce in MongoDB:
In addition, there are many materials for reference, such as MongoDBAggregationIII:Map-ReduceBasics.
This is the end of the content of "what's wrong with PHP operating MongoDB". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.