mongodb-odm-docs-dash/Doctrine ODM.docset/Contents/Resources/Documents/reference/map-reduce.html

242 lines
14 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Map Reduce &mdash; Doctrine MongoDB ODM 1.1.5 documentation</title>
<link rel="stylesheet" href="../_static/bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/layout.css" type="text/css" />
<link rel="stylesheet" href="../_static/configurationblock.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '1.1.5',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script src="../_static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
$("#versions").change(function() {
var docsUrl = $(this).val();
window.location.href = docsUrl;
});
});
-->
</script>
<link rel="shortcut icon" href="../_static/doctrine.ico"/>
<link rel="search" title="Search" href="../search.html" />
<link rel="top" title="Doctrine MongoDB ODM 1.1.5 documentation" href="../index.html" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h1 id="h1title"></h1>
<div id="logo">
<a href="http://www.doctrine-project.org/">Doctrine - PHP Database Libraries</a>
</div>
</div>
<div id="nav" class="cls">
<div class="tl cls">
<ul>
<li><a target="_top" href="http://www.doctrine-project.org/">Home</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/about.html">About</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/projects.html">Projects</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/contribute.html">Contribute</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/community.html">Community</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/archive.html">Blog</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/jira">Development</a></li>
</ul>
</div>
</div>
<div id="content" class="cls">
<div class="related">
<h3>Navigation</h3>
<ul>
<li><a href="/">Doctrine Homepage</a> &raquo;</li>
<li><a href="../index.html">Doctrine MongoDB ODM 1.1.5 documentation</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" >
<div class="section" id="map-reduce">
<h1>Map Reduce<a class="headerlink" href="#map-reduce" title="Permalink to this headline"></a></h1>
<p>The Doctrine MongoDB ODM fully supports the <a class="reference external" href="https://docs.mongodb.com/manual/core/map-reduce/">map reduce</a> functionality via its
<a class="reference internal" href="query-builder-api.html"><span class="doc">Query Builder API</span></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>From the MongoDB manual:</p>
<p class="last">Map-reduce is a data processing paradigm for condensing large volumes of
data into useful aggregated results. In MongoDB, map-reduce operations use
custom JavaScript functions to map, or associate, values to a key. If a key
has multiple values mapped to it, the operation reduces the values for the
key to a single object.</p>
</div>
<p>Imagine a situation where you had an application with a document
named <code class="docutils literal"><span class="pre">Event</span></code> and it was related to a <code class="docutils literal"><span class="pre">User</span></code> document:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">namespace</span> <span class="nx">Documents</span><span class="p">;</span>
<span class="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">Event</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @ReferenceOne(targetDocument=&quot;Documents\User&quot;) */</span>
<span class="k">private</span> <span class="nv">$user</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$type</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;date&quot;) */</span>
<span class="k">private</span> <span class="nv">$date</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$description</span><span class="p">;</span>
<span class="c1">// getters and setters</span>
<span class="p">}</span>
<span class="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">User</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>We may have a situation where we want to run a query that tells us how many
sales events each user has had. We can easily use the map reduce functionality
of MongoDB via the ODM's query builder. Here is a simple map reduce example:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">&#39;Documents\User&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">equals</span><span class="p">(</span><span class="s1">&#39;sale&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">map</span><span class="p">(</span><span class="s1">&#39;function() { emit(this.user.$id, 1); }&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">reduce</span><span class="p">(</span><span class="s1">&#39;function(k, vals) {</span>
<span class="s1"> var sum = 0;</span>
<span class="s1"> for (var i in vals) {</span>
<span class="s1"> sum += vals[i];</span>
<span class="s1"> }</span>
<span class="s1"> return sum;</span>
<span class="s1"> }&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$qb</span><span class="o">-&gt;</span><span class="na">getQuery</span><span class="p">();</span>
<span class="nv">$results</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">();</span>
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$results</span> <span class="k">as</span> <span class="nv">$user</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">printf</span><span class="p">(</span><span class="s2">&quot;User %s had %d sale(s).</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">,</span> <span class="nv">$user</span><span class="p">[</span><span class="s1">&#39;_id&#39;</span><span class="p">],</span> <span class="nv">$user</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]);</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The query builder also has a <code class="docutils literal"><span class="pre">finalize()</span></code> method, which may be used to
specify a <a class="reference external" href="https://docs.mongodb.com/master/reference/command/mapReduce/#mapreduce-finalize-cmd">finalize function</a> to be executed after the reduce step.</p>
</div>
<p>When using map reduce with Doctrine, the results are not hydrated into objects.
Instead, the raw results are returned directly from MongoDB.</p>
<p>The preceding example is equivalent to executing the following command via the
PHP driver directly:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$db</span> <span class="o">=</span> <span class="nv">$mongoClient</span><span class="o">-&gt;</span><span class="na">selectDB</span><span class="p">(</span><span class="s1">&#39;my_db&#39;</span><span class="p">);</span>
<span class="nv">$map</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MongoCode</span><span class="p">(</span><span class="s1">&#39;function() { emit(this.user.$id, 1); }&#39;</span><span class="p">);</span>
<span class="nv">$reduce</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MongoCode</span><span class="p">(</span><span class="s1">&#39;function(k, vals) {</span>
<span class="s1"> var sum = 0;</span>
<span class="s1"> for (var i in vals) {</span>
<span class="s1"> sum += vals[i];</span>
<span class="s1"> }</span>
<span class="s1"> return sum;</span>
<span class="s1">}&#39;</span><span class="p">);</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">command</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
<span class="s1">&#39;mapreduce&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;events&#39;</span><span class="p">,</span>
<span class="s1">&#39;map&#39;</span> <span class="o">=&gt;</span> <span class="nv">$map</span><span class="p">,</span>
<span class="s1">&#39;reduce&#39;</span> <span class="o">=&gt;</span> <span class="nv">$reduce</span><span class="p">,</span>
<span class="s1">&#39;query&#39;</span> <span class="o">=&gt;</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;type&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;sale&#39;</span><span class="p">),</span>
<span class="p">));</span>
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$result</span><span class="p">[</span><span class="s1">&#39;results&#39;</span><span class="p">]</span> <span class="k">as</span> <span class="nv">$user</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">printf</span><span class="p">(</span><span class="s2">&quot;User %s had %d sale(s).</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">,</span> <span class="nv">$user</span><span class="p">[</span><span class="s1">&#39;_id&#39;</span><span class="p">],</span> <span class="nv">$user</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="">
<h3>Search</h3>
<form class="search" action="http://readthedocs.org/search/project/" method="get">
<input type="text" name="q" size="18">
<input type="submit" value="Go">
<input type="hidden" name="selected_facets" value="project:">
</form>
</div>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/map-reduce.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy; Copyright 2013, Doctrine Project Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.2.
<br/>
<a target="_BLANK" href="http://www.servergrove.com"><img src="http://www.doctrine-project.org/_static/servergrove.jpg" /></a> <br/><br/>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick" />
<input type="hidden" name="hosted_button_id" value="BAE2E3XANQ77Y" />
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" />
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
</form>
</div>
</div>
<div id="bot-rcnr">
<div class="tl"><!-- corner --></div>
</div>
</div>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-288343-7";
urchinTracker();
</script>
<a class="githublink" href="http://github.com/doctrine"><img src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
</body>
</html>