1014 lines
83 KiB
HTML
1014 lines
83 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>Query Builder API — 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> »</li>
|
|
<li><a href="../index.html">Doctrine MongoDB ODM 1.1.5 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
|
|
<div class="body" >
|
|
|
|
<div class="section" id="query-builder-api">
|
|
<h1>Query Builder API<a class="headerlink" href="#query-builder-api" title="Permalink to this headline">¶</a></h1>
|
|
<p>Querying for documents with Doctrine is just as simple as if you
|
|
weren't using Doctrine at all. Of course you always have your
|
|
traditional <code class="docutils literal"><span class="pre">find()</span></code> and <code class="docutils literal"><span class="pre">findOne()</span></code> methods but you also have
|
|
a <code class="docutils literal"><span class="pre">Query</span></code> object with a fluent API for defining the query that
|
|
should be executed.</p>
|
|
<p>The <code class="docutils literal"><span class="pre">Query</span></code> object supports several types of queries</p>
|
|
<ul class="simple">
|
|
<li>FIND</li>
|
|
<li>FIND_AND_UPDATE</li>
|
|
<li>FIND_AND_REMOVE</li>
|
|
<li>INSERT</li>
|
|
<li>UPDATE</li>
|
|
<li>REMOVE</li>
|
|
<li>GROUP</li>
|
|
<li>MAP_REDUCE</li>
|
|
<li>DISTINCT_FIELD</li>
|
|
<li>GEO_LOCATION</li>
|
|
</ul>
|
|
<p>This section will show examples for the different types of queries.</p>
|
|
<div class="section" id="finding-documents">
|
|
<h2>Finding Documents<a class="headerlink" href="#finding-documents" title="Permalink to this headline">¶</a></h2>
|
|
<p>You have a few different ways to find documents. You can use the <code class="docutils literal"><span class="pre">find()</span></code> method
|
|
to find a document by its identifier:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">find</span><span class="p">(</span><span class="s1">'User'</span><span class="p">,</span> <span class="nv">$id</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The <code class="docutils literal"><span class="pre">find()</span></code> method is just a convenience shortcut method to:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getRepository</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span><span class="o">-></span><span class="na">find</span><span class="p">(</span><span class="nv">$id</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">The <code class="docutils literal"><span class="pre">find()</span></code> method checks the local in memory identity map for the document
|
|
before querying the database for the document.</p>
|
|
</div>
|
|
<p>On the <code class="docutils literal"><span class="pre">DocumentRepository</span></code> you have a few other methods for finding documents:</p>
|
|
<ul class="simple">
|
|
<li><code class="docutils literal"><span class="pre">findBy</span></code> - find documents by an array of criteria</li>
|
|
<li><code class="docutils literal"><span class="pre">findOneBy</span></code> - find one document by an array of criteria</li>
|
|
</ul>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getRepository</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span><span class="o">-></span><span class="na">findBy</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'type'</span> <span class="o">=></span> <span class="s1">'employee'</span><span class="p">));</span>
|
|
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getRepository</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span><span class="o">-></span><span class="na">findOneBy</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'jwage'</span><span class="p">));</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="creating-a-query-builder">
|
|
<h2>Creating a Query Builder<a class="headerlink" href="#creating-a-query-builder" title="Permalink to this headline">¶</a></h2>
|
|
<p>You can easily create a new <code class="docutils literal"><span class="pre">Query\Builder</span></code> object with the
|
|
<code class="docutils literal"><span class="pre">DocumentManager::createQueryBuilder()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The first and only argument is optional, you can specify it later
|
|
with the <code class="docutils literal"><span class="pre">find()</span></code>, <code class="docutils literal"><span class="pre">update()</span></code> (deprecated), <code class="docutils literal"><span class="pre">updateOne()</span></code>,
|
|
<code class="docutils literal"><span class="pre">updateMany()</span></code> or <code class="docutils literal"><span class="pre">remove()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">();</span>
|
|
|
|
<span class="c1">// ...</span>
|
|
|
|
<span class="nv">$qb</span><span class="o">-></span><span class="na">find</span><span class="p">(</span><span class="s1">'User'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="section" id="executing-queries">
|
|
<h3>Executing Queries<a class="headerlink" href="#executing-queries" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can execute a query by getting a <code class="docutils literal"><span class="pre">Query</span></code> through the <code class="docutils literal"><span class="pre">getQuery()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">);</span>
|
|
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$qb</span><span class="o">-></span><span class="na">getQuery</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Now you can <code class="docutils literal"><span class="pre">execute()</span></code> that query and it will return a cursor for you to iterate over the results:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="debugging-queries">
|
|
<h3>Debugging Queries<a class="headerlink" href="#debugging-queries" title="Permalink to this headline">¶</a></h3>
|
|
<p>While building not complicated queries is really simple sometimes it might be hard to wrap your head
|
|
around more sophisticated queries that involves building separate expressions to work properly. If
|
|
you are not sure if your the query constructed with Builder is in fact correct you may want to <code class="docutils literal"><span class="pre">debug()</span></code> it</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">);</span>
|
|
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$qb</span><span class="o">-></span><span class="na">getQuery</span><span class="p">();</span>
|
|
<span class="nv">$debug</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">debug</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>At this point your query is <em>prepared</em> - that means ODM done all its job in renaming fields to match their
|
|
database name, added discriminator fields, applied filters, created correct references and all other things
|
|
you employ ODM to. The array returned by <code class="docutils literal"><span class="pre">->debug()</span></code> is what is passed to the underlying driver for the
|
|
query to be performed.</p>
|
|
</div>
|
|
<div class="section" id="eager-cursors">
|
|
<h3>Eager Cursors<a class="headerlink" href="#eager-cursors" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can configure queries to return an eager cursor instead of a normal mongodb cursor using the <code class="docutils literal"><span class="pre">Builder#eagerCursor()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">eagerCursor</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
|
|
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$qb</span><span class="o">-></span><span class="na">getQuery</span><span class="p">();</span>
|
|
<span class="nv">$cursor</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">execute</span><span class="p">();</span> <span class="c1">// instanceof Doctrine\ODM\MongoDB\EagerCursor</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Iterating over the <code class="docutils literal"><span class="pre">$cursor</span></code> will fetch all the data in a short and small cursor all at once and will hydrate
|
|
one document at a time in to an object as you iterate:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$cursor</span> <span class="k">as</span> <span class="nv">$user</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// queries for all users and data is held internally</span>
|
|
<span class="c1">// each User object is hydrated from the data one at a time.</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="getting-single-result">
|
|
<h3>Getting Single Result<a class="headerlink" href="#getting-single-result" title="Permalink to this headline">¶</a></h3>
|
|
<p>If you want to just get a single result you can use the <code class="docutils literal"><span class="pre">Query#getSingleResult()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'username'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'jwage'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">getSingleResult</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="selecting-fields">
|
|
<h3>Selecting Fields<a class="headerlink" href="#selecting-fields" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can limit the fields that are returned in the results by using
|
|
the <code class="docutils literal"><span class="pre">select()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">select</span><span class="p">(</span><span class="s1">'username'</span><span class="p">,</span> <span class="s1">'password'</span><span class="p">);</span>
|
|
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$qb</span><span class="o">-></span><span class="na">getQuery</span><span class="p">();</span>
|
|
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>In the results only the data from the username and password will be
|
|
returned.</p>
|
|
</div>
|
|
<div class="section" id="index-hints">
|
|
<h3>Index hints<a class="headerlink" href="#index-hints" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can force MongoDB to use a specific index for a query with the <code class="docutils literal"><span class="pre">hint()</span></code> method (see <a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/meta/hint/">hint</a>)</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">hint</span><span class="p">(</span><span class="s1">'user_pass_idx'</span><span class="p">);</span>
|
|
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$qb</span><span class="o">-></span><span class="na">getQuery</span><span class="p">();</span>
|
|
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">Combining <code class="docutils literal"><span class="pre">select()</span></code> and <code class="docutils literal"><span class="pre">hint()</span></code> on appropriate indexes can result in very fast
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/core/query-optimization/#covered-query">covered queries</a></p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="selecting-distinct-values">
|
|
<h3>Selecting Distinct Values<a class="headerlink" href="#selecting-distinct-values" title="Permalink to this headline">¶</a></h3>
|
|
<p>Sometimes you may want to get an array of distinct values in a
|
|
collection. You can accomplish this using the <code class="docutils literal"><span class="pre">distinct()</span></code>
|
|
method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$ages</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">distinct</span><span class="p">(</span><span class="s1">'age'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The above would give you an <code class="docutils literal"><span class="pre">ArrayCollection</span></code> of all the distinct user ages!</p>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">MongoDB's <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/distinct/">distinct command</a>
|
|
does not support sorting, so you cannot combine <code class="docutils literal"><span class="pre">distinct()</span></code> with
|
|
<code class="docutils literal"><span class="pre">sort()</span></code>. If you would like to sort the results of a distinct query, you
|
|
will need to do so in PHP after executing the query.</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="refreshing-documents">
|
|
<h3>Refreshing Documents<a class="headerlink" href="#refreshing-documents" title="Permalink to this headline">¶</a></h3>
|
|
<p>When a query (e.g. geoNear, find) returns one or more hydrated documents whose
|
|
identifiers are already in the identity map, ODM returns the managed document
|
|
instances for those results. In this case, a managed document's data may differ
|
|
from whatever was just returned by the database query.</p>
|
|
<p>The query builder's <code class="docutils literal"><span class="pre">refresh()</span></code> method may be used to instruct ODM to override
|
|
the managed document with data from the query result. This is comparable to
|
|
calling <code class="docutils literal"><span class="pre">DocumentManager::refresh()</span></code> for a managed document. The document's
|
|
changeset will be reset in the process.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'username'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'jwage'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">refresh</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">getSingleResult</span><span class="p">();</span>
|
|
|
|
<span class="c1">// Jon's user will have the latest data, even if it was already managed</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Refreshing is not applicable if hydration is disabled.</p>
|
|
</div>
|
|
<div class="section" id="fetching-documents-as-read-only">
|
|
<h3>Fetching Documents as Read-Only<a class="headerlink" href="#fetching-documents-as-read-only" title="Permalink to this headline">¶</a></h3>
|
|
<p>Similar to <code class="docutils literal"><span class="pre">refresh()</span></code>, <code class="docutils literal"><span class="pre">readOnly()</span></code> instructs ODM to not only hydrate the
|
|
latest data but also to create new document's instance (i.e. if found document
|
|
would be already managed by Doctrine, new instance will be returned) and not
|
|
register it in <code class="docutils literal"><span class="pre">UnitOfWork</span></code>.</p>
|
|
<p>This technique can prove especially useful when using <code class="docutils literal"><span class="pre">select()</span></code> with no intent
|
|
to update fetched documents.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'username'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'malarzm'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">readOnly</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">getSingleResult</span><span class="p">();</span>
|
|
|
|
<span class="c1">// Maciej's user will have the latest data, and will not be the same object</span>
|
|
<span class="c1">// as the one that was already managed (if it was)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read-Only is not applicable if hydration is disabled.</p>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">Read-only mode is not deep, i.e. any references (be it owning or inverse) of
|
|
fetched WILL be managed by Doctrine. This is a shortcoming of current
|
|
implementation, may change in future and will not be considered a BC break
|
|
(will be treated as a feature instead).</p>
|
|
</div>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">To manage a document previously fetched in read-only mode, always use the
|
|
<cite>merge</cite> method of the DocumentManager. Using <cite>persist</cite> in these cases can
|
|
have unwanted side effects.</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="disabling-hydration">
|
|
<h3>Disabling Hydration<a class="headerlink" href="#disabling-hydration" title="Permalink to this headline">¶</a></h3>
|
|
<p>For find queries the results by default are hydrated and you get
|
|
document objects back instead of arrays. You can disable this and
|
|
get the raw results directly back from mongo by using the
|
|
<code class="docutils literal"><span class="pre">hydrate(false)</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$users</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">hydrate</span><span class="p">(</span><span class="k">false</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
|
|
<span class="nb">print_r</span><span class="p">(</span><span class="nv">$users</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="limiting-results">
|
|
<h3>Limiting Results<a class="headerlink" href="#limiting-results" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can limit results similar to how you would in a relational
|
|
database with a limit and offset by using the <code class="docutils literal"><span class="pre">limit()</span></code> and
|
|
<code class="docutils literal"><span class="pre">skip()</span></code> method.</p>
|
|
<p>Here is an example where we get the third page of blog posts when
|
|
we show twenty at a time:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$blogPosts</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'BlogPost'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">limit</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">skip</span><span class="p">(</span><span class="mi">40</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="sorting-results">
|
|
<h3>Sorting Results<a class="headerlink" href="#sorting-results" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can sort the results by using the <code class="docutils literal"><span class="pre">sort()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">sort</span><span class="p">(</span><span class="s1">'createdAt'</span><span class="p">,</span> <span class="s1">'desc'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you want to an additional sort you can call <code class="docutils literal"><span class="pre">sort()</span></code> again. The calls are stacked and ordered
|
|
in the order you call the method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$query</span><span class="o">-></span><span class="na">sort</span><span class="p">(</span><span class="s1">'featured'</span><span class="p">,</span> <span class="s1">'desc'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="map-reduce">
|
|
<h3>Map Reduce<a class="headerlink" href="#map-reduce" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can also run map reduced find queries using the <code class="docutils literal"><span class="pre">Query</span></code>
|
|
object:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Event'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'type'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'sale'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">map</span><span class="p">(</span><span class="s1">'function() { emit(this.userId, 1); }'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">reduce</span><span class="p">(</span><span class="s2">"function(k, vals) {</span>
|
|
<span class="s2"> var sum = 0;</span>
|
|
<span class="s2"> for (var i in vals) {</span>
|
|
<span class="s2"> sum += vals[i];</span>
|
|
<span class="s2"> }</span>
|
|
<span class="s2"> return sum;</span>
|
|
<span class="s2"> }"</span><span class="p">);</span>
|
|
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$qb</span><span class="o">-></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">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">When you specify a <code class="docutils literal"><span class="pre">map()</span></code> and <code class="docutils literal"><span class="pre">reduce()</span></code> operation
|
|
the results will not be hydrated and the raw results from the map
|
|
reduce operation will be returned.</p>
|
|
</div>
|
|
<p>If you just want to reduce the results using a javascript function
|
|
you can just call the <code class="docutils literal"><span class="pre">where()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">where</span><span class="p">(</span><span class="s2">"function() { return this.type == 'admin'; }"</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>You can read more about the <a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/where/">$where operator</a> in the Mongo docs.</p>
|
|
</div>
|
|
<div class="section" id="conditional-operators">
|
|
<h3>Conditional Operators<a class="headerlink" href="#conditional-operators" title="Permalink to this headline">¶</a></h3>
|
|
<p>The conditional operators in Mongo are available to limit the returned results through a easy to use API. Doctrine abstracts this to a fluent object oriented interface with a fluent API. Here is a list of all the conditional operation methods you can use on the <cite>QueryBuilder</cite> object.</p>
|
|
<ul class="simple">
|
|
<li><code class="docutils literal"><span class="pre">where($javascript)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">in($values)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">notIn($values)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">equals($value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">notEqual($value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">gt($value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">gte($value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">lt($value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">lte($value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">range($start,</span> <span class="pre">$end)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">size($size)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">exists($bool)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">type($type)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">all($values)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">mod($mod)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">addOr($expr)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">references($document)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">includesReferenceTo($document)</span></code></li>
|
|
</ul>
|
|
<p>Query for active administrator users:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'type'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'admin'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'active'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Query for articles that have some tags:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags.name'</span><span class="p">)</span><span class="o">-></span><span class="na">in</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'tag1'</span><span class="p">,</span> <span class="s1">'tag2'</span><span class="p">));</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/in/">$in operator</a>
|
|
in the Mongo docs</p>
|
|
<p>Query for articles that do not have some tags:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags.name'</span><span class="p">)</span><span class="o">-></span><span class="na">notIn</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'tag3'</span><span class="p">));</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/nin/">$nin operator</a>
|
|
in the Mongo docs.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'type'</span><span class="p">)</span><span class="o">-></span><span class="na">notEqual</span><span class="p">(</span><span class="s1">'admin'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/ne/">$ne operator</a>
|
|
in the Mongo docs.</p>
|
|
<p>Query for accounts with an amount due greater than 30:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Account'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'amount_due'</span><span class="p">)</span><span class="o">-></span><span class="na">gt</span><span class="p">(</span><span class="mi">30</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Query for accounts with an amount due greater than or equal to 30:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Account'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'amount_due'</span><span class="p">)</span><span class="o">-></span><span class="na">gte</span><span class="p">(</span><span class="mi">30</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Query for accounts with an amount due less than 30:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Account'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'amount_due'</span><span class="p">)</span><span class="o">-></span><span class="na">lt</span><span class="p">(</span><span class="mi">30</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Query for accounts with an amount due less than or equal to 30:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Account'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'amount_due'</span><span class="p">)</span><span class="o">-></span><span class="na">lte</span><span class="p">(</span><span class="mi">30</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Query for accounts with an amount due between 10 and 20:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Account'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'amount_due'</span><span class="p">)</span><span class="o">-></span><span class="na">range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about
|
|
<a class="reference external" href="http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ConditionalOperators%3A%3C%2C%3C%3D%2C%3E%2C%3E%3D">conditional operators</a>
|
|
in the Mongo docs.</p>
|
|
<p>Query for articles with no comments:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'comments'</span><span class="p">)</span><span class="o">-></span><span class="na">size</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/size/">$size operator</a>
|
|
in the Mongo docs.</p>
|
|
<p>Query for users that have a login field before it was renamed to
|
|
username:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'login'</span><span class="p">)</span><span class="o">-></span><span class="na">exists</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/exists/">$exists operator</a>
|
|
in the Mongo docs.</p>
|
|
<p>Query for users that have a type field that is of integer bson
|
|
type:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'type'</span><span class="p">)</span><span class="o">-></span><span class="na">type</span><span class="p">(</span><span class="s1">'integer'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/type/">$type operator</a>
|
|
in the Mongo docs.</p>
|
|
<p>Query for users that are in all the specified Groups:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'groups'</span><span class="p">)</span><span class="o">-></span><span class="na">all</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'Group 1'</span><span class="p">,</span> <span class="s1">'Group 2'</span><span class="p">));</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/all/">$all operator</a>
|
|
in the Mongo docs.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Transaction'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'field'</span><span class="p">)</span><span class="o">-></span><span class="na">mod</span><span class="p">(</span><span class="s1">'field'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="p">));</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/mod/">$mod operator</a> in the Mongo docs.</p>
|
|
<p>Query for users who have subscribed or are in a trial.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">);</span>
|
|
<span class="nv">$qb</span><span class="o">-></span><span class="na">addOr</span><span class="p">(</span><span class="nv">$qb</span><span class="o">-></span><span class="na">expr</span><span class="p">()</span><span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'subscriber'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="k">true</span><span class="p">));</span>
|
|
<span class="nv">$qb</span><span class="o">-></span><span class="na">addOr</span><span class="p">(</span><span class="nv">$qb</span><span class="o">-></span><span class="na">expr</span><span class="p">()</span><span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'inTrial'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="k">true</span><span class="p">));</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/or/">$or operator</a> in the Mongo docs.</p>
|
|
<p>The <code class="docutils literal"><span class="pre">references()</span></code> method may be used to query the owning side of a
|
|
<a class="reference internal" href="annotations-reference.html#annotations-reference-reference-one"><span class="std std-ref">@ReferenceOne</span></a> relationship. In the
|
|
following example, we query for all articles written by a particular user.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="c1">// Suppose $user has already been fetched from the database</span>
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'user'</span><span class="p">)</span><span class="o">-></span><span class="na">references</span><span class="p">(</span><span class="nv">$user</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The <code class="docutils literal"><span class="pre">includesReferenceTo()</span></code> method may be used to query the owning side of a
|
|
<a class="reference internal" href="annotations-reference.html#annotations-reference-reference-many"><span class="std std-ref">@ReferenceMany</span></a> relationship. In
|
|
the following example, we query for the user(s) that have access to a particular
|
|
account.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="c1">// Suppose $account has already been fetched from the database</span>
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'accounts'</span><span class="p">)</span><span class="o">-></span><span class="na">includesReferenceTo</span><span class="p">(</span><span class="nv">$account</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="text-search">
|
|
<h3>Text Search<a class="headerlink" href="#text-search" title="Permalink to this headline">¶</a></h3>
|
|
<p>You can use the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/text/">$text operator</a>
|
|
to run a text search against a field with a text index. To do so, create a
|
|
document with a text index:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @Document</span>
|
|
<span class="sd"> * @Index(keys={"description"="text"})</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">class</span> <span class="nc">Document</span>
|
|
<span class="p">{</span>
|
|
<span class="sd">/** @Id */</span>
|
|
<span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>
|
|
|
|
<span class="sd">/** @Field(type="string") */</span>
|
|
<span class="k">public</span> <span class="nv">$description</span><span class="p">;</span>
|
|
|
|
<span class="sd">/** @Field(type="float") @NotSaved */</span>
|
|
<span class="k">public</span> <span class="nv">$score</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>You can then run queries using the text operator:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="c1">// Run a text search against the index</span>
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Document'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">text</span><span class="p">(</span><span class="s1">'words you are looking for'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>To fetch the calculated score for the text search, use the <code class="docutils literal"><span class="pre">selectMeta()</span></code>
|
|
method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="c1">// Run a text search against the index</span>
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Document'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">selectMeta</span><span class="p">(</span><span class="s1">'score'</span><span class="p">,</span> <span class="s1">'textScore'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">text</span><span class="p">(</span><span class="s1">'words you are looking for'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>You can also change the language used for stemming using the <code class="docutils literal"><span class="pre">language()</span></code>
|
|
method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="c1">// Run a text search against the index</span>
|
|
<span class="nv">$qb</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Document'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">language</span><span class="p">(</span><span class="s1">'it'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">text</span><span class="p">(</span><span class="s1">'parole che stai cercando'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="update-queries">
|
|
<h3>Update Queries<a class="headerlink" href="#update-queries" title="Permalink to this headline">¶</a></h3>
|
|
<p>Doctrine also supports executing atomic update queries using the <cite>QueryBuilder</cite>
|
|
object. You can use the conditional operations in combination with the ability to
|
|
change document field values atomically. Additionally if you are modifying a field
|
|
that is a reference you can pass managed document to the Builder and let ODM build
|
|
<code class="docutils literal"><span class="pre">DBRef</span></code> object for you.</p>
|
|
<p>You have several modifier operations
|
|
available to you that make it easy to update documents in Mongo:</p>
|
|
<ul class="simple">
|
|
<li><code class="docutils literal"><span class="pre">set($name,</span> <span class="pre">$value,</span> <span class="pre">$atomic</span> <span class="pre">=</span> <span class="pre">true)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">setNewObj($newObj)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">inc($name,</span> <span class="pre">$value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">unsetField($field)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">push($field,</span> <span class="pre">$value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">pushAll($field,</span> <span class="pre">array</span> <span class="pre">$valueArray)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">addToSet($field,</span> <span class="pre">$value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">addManyToSet($field,</span> <span class="pre">array</span> <span class="pre">$values)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">popFirst($field)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">popLast($field)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">pull($field,</span> <span class="pre">$value)</span></code></li>
|
|
<li><code class="docutils literal"><span class="pre">pullAll($field,</span> <span class="pre">array</span> <span class="pre">$valueArray)</span></code></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="updating-multiple-documents">
|
|
<h2>Updating multiple documents<a class="headerlink" href="#updating-multiple-documents" title="Permalink to this headline">¶</a></h2>
|
|
<p>By default Mongo updates only one document unless <code class="docutils literal"><span class="pre">multi</span></code> option is provided and true.
|
|
In ODM the distinction is done by explicitly calling <code class="docutils literal"><span class="pre">updateMany()</span></code> method of the builder:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateMany</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'someField'</span><span class="p">)</span><span class="o">-></span><span class="na">set</span><span class="p">(</span><span class="s1">'newValue'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'username'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'sgoettschkes'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last"><code class="docutils literal"><span class="pre">updateMany()</span></code> and <code class="docutils literal"><span class="pre">updateOne()</span></code> methods were introduced in version 1.2. If you're
|
|
using one of previous version you need to use <code class="docutils literal"><span class="pre">update()</span></code> combined with <code class="docutils literal"><span class="pre">multiple(true)</span></code>.</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="modifier-operations">
|
|
<h2>Modifier Operations<a class="headerlink" href="#modifier-operations" title="Permalink to this headline">¶</a></h2>
|
|
<p>Change a users password:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateOne</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'password'</span><span class="p">)</span><span class="o">-></span><span class="na">set</span><span class="p">(</span><span class="s1">'newpassword'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'username'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'jwage'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you want to just set the values of an entirely new object you
|
|
can do so by passing false as the third argument of <code class="docutils literal"><span class="pre">set()</span></code> to
|
|
tell it the update is not an atomic one:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateOne</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'username'</span><span class="p">)</span><span class="o">-></span><span class="na">set</span><span class="p">(</span><span class="s1">'jwage'</span><span class="p">,</span> <span class="k">false</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'password'</span><span class="p">)</span><span class="o">-></span><span class="na">set</span><span class="p">(</span><span class="s1">'password'</span><span class="p">,</span> <span class="k">false</span><span class="p">)</span>
|
|
<span class="c1">// ... set other remaining fields</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'username'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'jwage'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/set/">$set modifier</a>
|
|
in the Mongo docs.</p>
|
|
<p>You can set an entirely new object to update as well:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">setNewObj</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
|
|
<span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'jwage'</span><span class="p">,</span>
|
|
<span class="s1">'password'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span>
|
|
<span class="c1">// ... other fields</span>
|
|
<span class="p">))</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'username'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'jwage'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Increment the value of a document:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Package'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'theid'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'downloads'</span><span class="p">)</span><span class="o">-></span><span class="na">inc</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/inc/">$inc modifier</a>
|
|
in the Mongo docs.</p>
|
|
<p>Unset the login field from users where the login field still
|
|
exists:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateMany</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'login'</span><span class="p">)</span><span class="o">-></span><span class="na">unsetField</span><span class="p">()</span><span class="o">-></span><span class="na">exists</span><span class="p">(</span><span class="k">true</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/unset/">$unset modifier</a>
|
|
in the Mongo docs.</p>
|
|
<p>Append new tag to the tags array:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateOne</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">)</span><span class="o">-></span><span class="na">push</span><span class="p">(</span><span class="s1">'tag5'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'theid'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/push/">$push modifier</a>
|
|
in the Mongo docs.</p>
|
|
<p>Append new tags to the tags array:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateOne</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">)</span><span class="o">-></span><span class="na">pushAll</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'tag6'</span><span class="p">,</span> <span class="s1">'tag7'</span><span class="p">))</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'theid'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/pushAll/">$pushAll modifier</a>
|
|
in the Mongo docs.</p>
|
|
<p>Add value to array only if its not in the array already:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateOne</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">)</span><span class="o">-></span><span class="na">addToSet</span><span class="p">(</span><span class="s1">'tag1'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'theid'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/addToSet/">$addToSet modifier</a>
|
|
in the Mongo docs.</p>
|
|
<p>Add many values to the array only if they do not exist in the array
|
|
already:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateOne</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">)</span><span class="o">-></span><span class="na">addManyToSet</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'tag6'</span><span class="p">,</span> <span class="s1">'tag7'</span><span class="p">))</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'theid'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="http://www.mongodb.org/display/DOCS/Updating#Updating-%24addManyToSet">$addManyToSet modifier</a>
|
|
in the Mongo docs.</p>
|
|
<p>Remove first element in an array:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateOne</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">)</span><span class="o">-></span><span class="na">popFirst</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'theid'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Remove last element in an array:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateOne</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">)</span><span class="o">-></span><span class="na">popLast</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="s1">'theid'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/pop/">$pop modifier</a>
|
|
in the Mongo docs.</p>
|
|
<p>Remove all occurrences of value from array:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateMany</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">)</span><span class="o">-></span><span class="na">pull</span><span class="p">(</span><span class="s1">'tag1'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/pull/">$pull modifier</a>
|
|
in the Mongo docs.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Article'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">updateMany</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">)</span><span class="o">-></span><span class="na">pullAll</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'tag1'</span><span class="p">,</span> <span class="s1">'tag2'</span><span class="p">))</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Read more about the
|
|
<a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/pullAll/">$pullAll modifier</a>
|
|
in the Mongo docs.</p>
|
|
</div>
|
|
<div class="section" id="remove-queries">
|
|
<h2>Remove Queries<a class="headerlink" href="#remove-queries" title="Permalink to this headline">¶</a></h2>
|
|
<p>In addition to updating you can also issue queries to remove
|
|
documents from a collection. It works pretty much the same way as
|
|
everything else and you can use the conditional operations to
|
|
specify which documents you want to remove.</p>
|
|
<p>Here is an example where we remove users who have never logged in:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">remove</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'num_logins'</span><span class="p">)</span><span class="o">-></span><span class="na">equals</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="group-queries">
|
|
<h2>Group Queries<a class="headerlink" href="#group-queries" title="Permalink to this headline">¶</a></h2>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">Due to deprecation of <code class="docutils literal"><span class="pre">group</span></code> command in MongoDB 3.4 the ODM
|
|
also deprecates its usage through Query Builder in 1.2. Please
|
|
use <a class="reference internal" href="aggregation-builder.html#aggregation-builder-group"><span class="std std-ref">$group stage</span></a> of the
|
|
Aggregation Builder instead.</p>
|
|
</div>
|
|
<p>The last type of supported query is a group query. It performs an
|
|
operation similar to SQL's GROUP BY command.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">dm</span><span class="o">-></span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">'Documents\User'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">group</span><span class="p">(</span><span class="k">array</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span><span class="s1">'count'</span> <span class="o">=></span> <span class="mi">0</span><span class="p">))</span>
|
|
<span class="o">-></span><span class="na">reduce</span><span class="p">(</span><span class="s1">'function (obj, prev) { prev.count++; }'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">field</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span><span class="o">-></span><span class="na">gt</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">getQuery</span><span class="p">()</span>
|
|
<span class="o">-></span><span class="na">execute</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>This is the same as if we were to do the group with the raw PHP
|
|
code:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$reduce</span> <span class="o">=</span> <span class="s1">'function (obj, prev) { prev.count++; }'</span><span class="p">;</span>
|
|
<span class="nv">$condition</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">'a'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span> <span class="s1">'$gt'</span> <span class="o">=></span> <span class="mi">1</span><span class="p">));</span>
|
|
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$collection</span><span class="o">-></span><span class="na">group</span><span class="p">(</span><span class="k">array</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span><span class="s1">'count'</span> <span class="o">=></span> <span class="mi">0</span><span class="p">),</span> <span class="nv">$reduce</span><span class="p">,</span> <span class="nv">$condition</span><span class="p">);</span>
|
|
</pre></div>
|
|
</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><a href="../index.html">Table Of Contents</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#">Query Builder API</a><ul>
|
|
<li><a class="reference internal" href="#finding-documents">Finding Documents</a></li>
|
|
<li><a class="reference internal" href="#creating-a-query-builder">Creating a Query Builder</a><ul>
|
|
<li><a class="reference internal" href="#executing-queries">Executing Queries</a></li>
|
|
<li><a class="reference internal" href="#debugging-queries">Debugging Queries</a></li>
|
|
<li><a class="reference internal" href="#eager-cursors">Eager Cursors</a></li>
|
|
<li><a class="reference internal" href="#getting-single-result">Getting Single Result</a></li>
|
|
<li><a class="reference internal" href="#selecting-fields">Selecting Fields</a></li>
|
|
<li><a class="reference internal" href="#index-hints">Index hints</a></li>
|
|
<li><a class="reference internal" href="#selecting-distinct-values">Selecting Distinct Values</a></li>
|
|
<li><a class="reference internal" href="#refreshing-documents">Refreshing Documents</a></li>
|
|
<li><a class="reference internal" href="#fetching-documents-as-read-only">Fetching Documents as Read-Only</a></li>
|
|
<li><a class="reference internal" href="#disabling-hydration">Disabling Hydration</a></li>
|
|
<li><a class="reference internal" href="#limiting-results">Limiting Results</a></li>
|
|
<li><a class="reference internal" href="#sorting-results">Sorting Results</a></li>
|
|
<li><a class="reference internal" href="#map-reduce">Map Reduce</a></li>
|
|
<li><a class="reference internal" href="#conditional-operators">Conditional Operators</a></li>
|
|
<li><a class="reference internal" href="#text-search">Text Search</a></li>
|
|
<li><a class="reference internal" href="#update-queries">Update Queries</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="reference internal" href="#updating-multiple-documents">Updating multiple documents</a></li>
|
|
<li><a class="reference internal" href="#modifier-operations">Modifier Operations</a></li>
|
|
<li><a class="reference internal" href="#remove-queries">Remove Queries</a></li>
|
|
<li><a class="reference internal" href="#group-queries">Group Queries</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../_sources/reference/query-builder-api.rst.txt"
|
|
rel="nofollow">Show Source</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="footer">
|
|
© 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> |