在线
客服

在线客服
尊敬的客户,我们24小时竭诚为您服务 公司总机: 0755-83312037 (32条线)

客服
热线

0755-83312037 (32条线)
7*24小时客服服务热线

?

关注
微信

关注官方微信

返回
顶部

PHP驱动MongoDB整数问题的BUG和策略

发布时间:2019-04-13浏览次数:846
<p> </p> <table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr> <td style="WORD-WRAP: break-word" bgcolor="#fdfddf"> <font color="#ff0000">WebjxCom提示:</font><font color="#000000">PHP驱动MongoDB整数问题的BUG和策略.</font> </td> </tr></tbody></table> <p>本文所说的<a href="http://jira.mongodb.org/browse/PHP-138" target="_blank"><font color="#0000ff">整数问题</font></a>,其实并不是MongoDB的问题,而是PHP驱动的问题:MongoDB本身有两种整数类型,分别是:32位整数和64位整数,但旧版的PHP驱动不管操作系统是32位还是64位,把所有整数都当做32位整数处理,结果导致64位整数被截断。为了在尽可能保持兼容性的前提下解决这个问题,新版PHP驱动加入了<a href="http://www.php.net/manual/en/mongo.configuration.php#ini.mongo.native-long" target="_blank"><font color="#0000ff">mongo.native-long</font></a>选项,以期在64位操作系统中把整数都当做64位来处理,有兴趣的可参考:<a href="http://derickrethans.nl/64bit-ints-in-mongodb.html" target="_blank"><font color="#0000ff">64-bit integers in MongoDB</font></a>。</p> <p>那么PHP驱动真的完全解决了整数问题么?NO!在处理group操作的时候还有<a href="http://jira.mongodb.org/browse/PHP-163" target="_blank"><font color="#0000ff">BUG</font></a>:</p> <p>为了说明问题,我们先来生成一些测试数据:</p> <p> </p> <table style="BORDER-BOTTOM: #0099cc 1px solid; BORDER-LEFT: #0099cc 1px solid; TABLE-LAYOUT: fixed; BORDER-TOP: #0099cc 1px solid; BORDER-RIGHT: #0099cc 1px solid" border="0" cellspacing="0" cellpadding="6" width="95%" align="center"><tbody><tr> <td style="WORD-WRAP: break-word" bgcolor="#ddedfb"> <p><code><font face="NSimsun">&lt;?php<br><br>ini_set('mongo.native_long', 1);<br><br>$instance = new Mongo();<br><br>$instance = $instance-&gt;selectCollection('test', 'test');<br><br>for ($i = 0; $i &lt; 10; $i++) {<br>&#160;&#160;&#160; $instance-&gt;insert(array(<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'group_id' =&gt; rand(1, 5),<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 'count'&#160;&#160;&#160; =&gt; rand(1, 5),<br>&#160;&#160;&#160; ));<br>}<br><br>?&gt;</font></code></p> </td> </tr></tbody></table> <p>下面让我们使用group操作,根据group_id分组,汇总计算count:</p> <p> </p> <table style="BORDER-BOTTOM: #0099cc 1px solid; BORDER-LEFT: #0099cc 1px solid; TABLE-LAYOUT: fixed; BORDER-TOP: #0099cc 1px solid; BORDER-RIGHT: #0099cc 1px solid" border="0" cellspacing="0" cellpadding="6" width="95%" align="center"><tbody><tr> <td style="WORD-WRAP: break-word" bgcolor="#ddedfb"> <p><code><font size="2" face="新宋体">&lt;?php<br><br>ini_set('mongo.native_long', 1);<br><br>$instance = new Mongo();<br><br>$instance = $instance-&gt;selectCollection('test', 'test');<br><br>$keys = array('group_id' =&gt; 1);<br><br>$initial = array('count' =&gt; 0);<br><br>$reduce = '<br>&#160;&#160;&#160; function(obj, prev) {<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160; prev.count += obj.count;<br>&#160;&#160;&#160; }<br>';<br><br>$result = $instance-&gt;group($keys, $initial, $reduce);<br><br>var_dump($result);<br><br>?&gt;</font></code></p> </td> </tr></tbody></table> <p>结果和预想的有出入,count没有实现累加,而是变成了[object Object],目前,如果必须使用group操作,那么有两种方法可以缓解这个问题:</p> <p> </p> <table style="BORDER-BOTTOM: #0099cc 1px solid; BORDER-LEFT: #0099cc 1px solid; TABLE-LAYOUT: fixed; BORDER-TOP: #0099cc 1px solid; BORDER-RIGHT: #0099cc 1px solid" border="0" cellspacing="0" cellpadding="6" width="95%" align="center"><tbody><tr> <td style="WORD-WRAP: break-word" bgcolor="#ddedfb"> <p><code><font size="2" face="新宋体">ini_set('mongo.native_long', 0);</font></code></p> </td> </tr></tbody></table> <p> </p> <table style="BORDER-BOTTOM: #0099cc 1px solid; BORDER-LEFT: #0099cc 1px solid; TABLE-LAYOUT: fixed; BORDER-TOP: #0099cc 1px solid; BORDER-RIGHT: #0099cc 1px solid" border="0" cellspacing="0" cellpadding="6" width="95%" align="center"><tbody><tr> <td style="WORD-WRAP: break-word" bgcolor="#ddedfb"> <p><code><font size="2" face="新宋体">$initial = array('count' =&gt; (float)0);</font></code></p> </td> </tr></tbody></table> <p>这两种方法都是治标不治本的权宜之计,既然当前PHP驱动里group的实现有问题,那我们就绕开它,用其它的方式实现同样的功能,这个方式就是<a href="http://www.mongodb.org/display/DOCS/MapReduce" target="_blank"><font color="#0000ff">MapReduce</font></a>:</p> <p> </p> <table style="BORDER-BOTTOM: #0099cc 1px solid; BORDER-LEFT: #0099cc 1px solid; TABLE-LAYOUT: fixed; BORDER-TOP: #0099cc 1px solid; BORDER-RIGHT: #0099cc 1px solid" border="0" cellspacing="0" cellpadding="6" width="95%" align="center"><tbody><tr> <td style="WORD-WRAP: break-word" bgcolor="#ddedfb"> <p><code><font size="2" face="新宋体">&lt;?php<br><br>ini_set('mongo.native_long', 1);<br><br>$instance = new Mongo();<br><br>$instance = $instance-&gt;selectDB('test');<br><br>$map = '<br>&#160;&#160;&#160; function() {<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160; emit(this.group_id, this.count);<br>&#160;&#160;&#160; }<br>';<br><br>$reduce = '<br>&#160;&#160;&#160; function(key, values) {<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160; var sum = 0;<br><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (var index in values) {<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; sum += values[index];<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return sum;<br>&#160;&#160;&#160; }<br>';<br><br>$result = $instance-&gt;command(array(<br>&#160;&#160;&#160; 'mapreduce' =&gt; 'test',<br>&#160;&#160;&#160; 'map'&#160;&#160;&#160;&#160;&#160;&#160; =&gt; $map,<br>&#160;&#160;&#160; 'reduce'&#160;&#160;&#160; =&gt; $reduce<br>));<br><br>$result = iterator_to_array($instance-&gt;{$result['result']}-&gt;find());<br><br>var_dump($result);<br><br>?&gt;</font></code></p> </td> </tr></tbody></table> <p>把大象放冰箱里需要三步,而使用MapReduce仅仅需要Map和Reduce两步即可,这里有一个PDF文档生动的说明了MySQL中GROUP BY和MongoDB中MapReduce的对应关系:</p> <p align="center"><a href="http://www.chinaz.com/upimg/userup/1103/1409294BM3.jpg" target="_blank"><img border="0" alt="" src="http://www.webjx.com/files/allimg/110324/1019180.jpg" width="570" height="441"></a>&#160;</p> <p align="center"><a href="http://rickosborne.org/blog/2010/02/infographic-migrating-from-sql-to-mapreduce-with-mongodb/" target="_blank"><font color="#0000ff">SQL to MongoDB</font></a></p> <p>此外,还有很多资料可供参考,如:<a href="http://kylebanker.com/blog/2009/12/mongodb-map-reduce-basics/" target="_blank"><font color="#0000ff">MongoDB Aggregation III: Map-Reduce Basics</font></a>。</p> <p>说明:软件版本为MongoDB(1.6.5),PECL Mongo(1.1.4)。不同版本结论可能不同。</p>