url
stringlengths 36
137
| content
stringlengths 1.21k
7.75M
|
---|---|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottomN/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$bottomN (aggregation accumulator)
<a class="headerlink leafygreen-ui-1kepx47" href="#-bottomn--aggregation-accumulator-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-bottomn--aggregation-accumulator-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#restrictions" target="_self">
<span>
Restrictions
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="group">
<dt>
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-group-grp.-bottomN" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-group-grp.-bottomN">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the bottom
<code class="leafygreen-ui-1l06pbn">
n
</code>
elements within a group,
according to the specified sort order. If the group contains fewer
than
<code class="leafygreen-ui-1l06pbn">
n
</code>
elements,
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
returns all elements in the group.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $bottomN:\n {\n n: <expression>,\n sortBy: { <field1>: <sort>, <field2>: <sort> ... },\n output: <expression>\n }\n}"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $bottomN:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> n: <expression>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> output: <expression></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
n
</code>
limits the number of results per group and has to be a positive
integral expression that is either a constant or depends on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
value for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
sortBy specifies the order of results, with syntax similar to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
<code class="leafygreen-ui-1l06pbn">
$sort
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
output
</code>
represents the output for each element in the group and can
be any expression.
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Null and Missing Values
<a class="headerlink leafygreen-ui-1kepx47" href="#null-and-missing-values" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="null-and-missing-values">
</div>
</a>
</h3>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
does not filter out null values.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
converts missing values to null which are preserved in
the output.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.aggregate( [\n {\n $documents: [\n { playerId: \"PlayerA\", gameId: \"G1\", score: 1 },\n { playerId: \"PlayerB\", gameId: \"G1\", score: 2 },\n { playerId: \"PlayerC\", gameId: \"G1\", score: 3 },\n { playerId: \"PlayerD\", gameId: \"G1\"},\n { playerId: \"PlayerE\", gameId: \"G1\", score: null }\n ]\n },\n {\n $group:\n {\n _id: \"$gameId\",\n playerId:\n {\n $bottomN:\n {\n output: [ \"$playerId\", \"$score\" ],\n sortBy: { \"score\": -1 },\n n: 3\n }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$documents</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerB"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">2</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerC"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">3</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerD"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>}<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerE"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-literal">null</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$group</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$gameId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">playerId</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$bottomN</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">output</span>: <!-- -->[ <span class="lg-highlight-string">"$playerId"</span>, <span class="lg-highlight-string">"$score"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-string">"score"</span>: <!-- -->-<span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In this example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
<code class="leafygreen-ui-1l06pbn">
$documents
</code>
</a>
creates the literal documents that contain
player scores.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
groups the documents by
<code class="leafygreen-ui-1l06pbn">
gameId
</code>
. This
example has only one
<code class="leafygreen-ui-1l06pbn">
gameId
</code>
,
<code class="leafygreen-ui-1l06pbn">
G1
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
PlayerD
</code>
has a missing score and
<code class="leafygreen-ui-1l06pbn">
PlayerE
</code>
has a
null
<code class="leafygreen-ui-1l06pbn">
score
</code>
. These values are both considered as null.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
playerId
</code>
and
<code class="leafygreen-ui-1l06pbn">
score
</code>
fields are specified as
<code class="leafygreen-ui-1l06pbn">
output : ["$playerId"," $score"]
</code>
and returned as array values.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Because of the
<code class="leafygreen-ui-1l06pbn">
sortBy: { "score" : -1 }
</code>
, the null values are sorted
to the end of the returned
<code class="leafygreen-ui-1l06pbn">
playerId
</code>
array.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: \"G1\",\n playerId: [ [ \"PlayerA\", 1 ], [ \"PlayerD\", null ], [ \"PlayerE\", null ] ]\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"G1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">playerId</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-number">1</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerD"</span>, <span class="lg-highlight-literal">null</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerE"</span>, <span class="lg-highlight-literal">null</span> ] ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
BSON Data Type Sort Ordering
<a class="headerlink leafygreen-ui-1kepx47" href="#bson-data-type-sort-ordering" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="bson-data-type-sort-ordering">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When sorting different types, the order of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#std-label-bson_sort_order">
BSON data types
</a>
is used to determine ordering. As an example,
consider a collection whose values consist of strings and numbers.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In an ascending sort, string values are sorted after numeric values.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In a descending sort, string values are sorted before numeric values.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.aggregate( [\n {\n $documents: [\n { playerId: \"PlayerA\", gameId: \"G1\", score: 1 },\n { playerId: \"PlayerB\", gameId: \"G1\", score: \"2\" },\n { playerId: \"PlayerC\", gameId: \"G1\", score: \"\" }\n ]\n },\n {\n $group:\n {\n _id: \"$gameId\",\n playerId: {\n $bottomN:\n {\n output: [\"$playerId\",\"$score\"],\n sortBy: {\"score\": -1},\n n: 3\n }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$documents</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerB"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-string">"2"</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerC"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-string">""</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$group</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$gameId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">playerId</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$bottomN</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">output</span>: <!-- -->[<span class="lg-highlight-string">"$playerId"</span>,<span class="lg-highlight-string">"$score"</span>]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{<span class="lg-highlight-string">"score"</span>: <!-- -->-<span class="lg-highlight-number">1</span>}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In this example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
PlayerA
</code>
has an integer score.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
PlayerB
</code>
has a string
<code class="leafygreen-ui-1l06pbn">
"2"
</code>
score.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
PlayerC
</code>
has an empty string score.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Because the sort is in descending
<code class="leafygreen-ui-1l06pbn">
{ "score" : -1 }
</code>
, the string
literal values are sorted before
<code class="leafygreen-ui-1l06pbn">
PlayerA
</code>
's numeric score:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: \"G1\",\n playerId: [ [ \"PlayerB\", \"2\" ], [ \"PlayerC\", \"\" ], [ \"PlayerA\", 1 ] ]\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"G1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">playerId</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"PlayerB"</span>, <span class="lg-highlight-string">"2"</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerC"</span>, <span class="lg-highlight-string">""</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-number">1</span> ] ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Restrictions
<a class="headerlink leafygreen-ui-1kepx47" href="#restrictions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="restrictions">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Window Function and Aggregation Expression Support
<a class="headerlink leafygreen-ui-1kepx47" href="#window-function-and-aggregation-expression-support" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="window-function-and-aggregation-expression-support">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
is not supported as a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
aggregation expression
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
is supported as a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
window operator
</code>
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Memory Limit Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#memory-limit-considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="memory-limit-considerations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Groups within the
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
aggregation pipeline are subject to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline-limits/#std-label-agg-memory-restrictions">
100 MB limit
</a>
pipeline limit. If this
limit is exceeded for an individual group, the aggregation fails
with an error.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Consider a
<code class="leafygreen-ui-1l06pbn">
gamescores
</code>
collection with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.gamescores.insertMany([\n { playerId: \"PlayerA\", gameId: \"G1\", score: 31 },\n { playerId: \"PlayerB\", gameId: \"G1\", score: 33 },\n { playerId: \"PlayerC\", gameId: \"G1\", score: 99 },\n { playerId: \"PlayerD\", gameId: \"G1\", score: 1 },\n { playerId: \"PlayerA\", gameId: \"G2\", score: 10 },\n { playerId: \"PlayerB\", gameId: \"G2\", score: 14 },\n { playerId: \"PlayerC\", gameId: \"G2\", score: 66 },\n { playerId: \"PlayerD\", gameId: \"G2\", score: 80 }\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">gamescores</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">31</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerB"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">33</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerC"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">99</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerD"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G2"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">10</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerB"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G2"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">14</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerC"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G2"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">66</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">playerId</span>: <span class="lg-highlight-string">"PlayerD"</span>, <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G2"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">80</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Find the Three Lowest
<code class="leafygreen-ui-1l06pbn">
Scores
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#find-the-three-lowest-scores" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="find-the-three-lowest-scores">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
accumulator to find the lowest scoring
players in a single game.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.gamescores.aggregate( [\n {\n $match : { gameId : \"G1\" }\n },\n {\n $group:\n {\n _id: \"$gameId\",\n playerId:\n {\n $bottomN:\n {\n output: [\"$playerId\", \"$score\"],\n sortBy: { \"score\": -1 },\n n:3\n }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">gamescores</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->$match<!-- --> <!-- -->: <!-- -->{ <!-- -->gameId <!-- -->: <span class="lg-highlight-string">"G1"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$group</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$gameId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">playerId</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$bottomN</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">output</span>: <!-- -->[<span class="lg-highlight-string">"$playerId"</span>, <span class="lg-highlight-string">"$score"</span>]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-string">"score"</span>: <!-- -->-<span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>:<span class="lg-highlight-number">3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The example pipeline:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
to filter the results on a single
<code class="leafygreen-ui-1l06pbn">
gameId
</code>
.
In this case,
<code class="leafygreen-ui-1l06pbn">
G1
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
to group the results by
<code class="leafygreen-ui-1l06pbn">
gameId
</code>
. In this
case,
<code class="leafygreen-ui-1l06pbn">
G1
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses sort by
<code class="leafygreen-ui-1l06pbn">
{ "score": -1 }
</code>
to sort the results in descending
order.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Specifies the fields that are output from
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
with
<code class="leafygreen-ui-1l06pbn">
output : ["$playerId"," $score"]
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
to return the bottom three documents
with the lowest
<code class="leafygreen-ui-1l06pbn">
score
</code>
for the
<code class="leafygreen-ui-1l06pbn">
G1
</code>
game with
<code class="leafygreen-ui-1l06pbn">
n : 3
</code>
.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: \"G1\",\n playerId: [ [ \"PlayerB\", 33 ], [ \"PlayerA\", 31 ], [ \"PlayerD\", 1 ] ]\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"G1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">playerId</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"PlayerB"</span>, <span class="lg-highlight-number">33</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-number">31</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerD"</span>, <span class="lg-highlight-number">1</span> ] ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The SQL equivalent to this query is:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT T3.GAMEID,T3.PLAYERID,T3.SCORE\nFROM GAMESCORES AS GS\nJOIN (SELECT TOP 3\n GAMEID,PLAYERID,SCORE\n FROM GAMESCORES\n WHERE GAMEID = \"G1\"\n ORDER BY SCORE) AS T3\n ON GS.GAMEID = T3.GAMEID\nGROUP BY T3.GAMEID,T3.PLAYERID,T3.SCORE\n ORDER BY T3.SCORE DESC","programmingLanguage":"SQL"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light sql leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">SELECT</span> T3.GAMEID,T3.PLAYERID,T3.SCORE</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> GAMESCORES <span class="lg-highlight-keyword">AS</span> GS</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">JOIN</span> (<span class="lg-highlight-keyword">SELECT</span> TOP <span class="lg-highlight-number">3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> GAMEID,PLAYERID,SCORE</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> GAMESCORES</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">WHERE</span> GAMEID <span class="lg-highlight-operator">=</span> <!-- -->"G1"</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">ORDER</span> <span class="lg-highlight-keyword">BY</span> SCORE) <span class="lg-highlight-keyword">AS</span> T3</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">ON</span> GS.GAMEID <span class="lg-highlight-operator">=</span> T3.GAMEID</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">GROUP</span> <span class="lg-highlight-keyword">BY</span> T3.GAMEID,T3.PLAYERID,T3.SCORE</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">ORDER</span> <span class="lg-highlight-keyword">BY</span> T3.SCORE <span class="lg-highlight-keyword">DESC</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Finding the Three Lowest Score Documents Across Multiple Games
<a class="headerlink leafygreen-ui-1kepx47" href="#finding-the-three-lowest-score-documents-across-multiple-games" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="finding-the-three-lowest-score-documents-across-multiple-games">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
accumulator to find the lowest scoring
players in each game.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.gamescores.aggregate( [\n {\n $group:\n { _id: \"$gameId\", playerId:\n {\n $bottomN:\n {\n output: [ \"$playerId\",\"$score\" ],\n sortBy: { \"score\": -1 },\n n: 3\n }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">gamescores</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$group</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$gameId"</span>, <span class="lg-highlight-attr">playerId</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$bottomN</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">output</span>: <!-- -->[ <span class="lg-highlight-string">"$playerId"</span>,<span class="lg-highlight-string">"$score"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-string">"score"</span>: <!-- -->-<span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The example pipeline:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses
<code class="leafygreen-ui-1l06pbn">
$group
</code>
to group the results by
<code class="leafygreen-ui-1l06pbn">
gameId
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Specifies the fields that are output from
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
with
<code class="leafygreen-ui-1l06pbn">
output : ["$playerId", "$score"]
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses sort by
<code class="leafygreen-ui-1l06pbn">
{ "score": -1 }
</code>
to sort the results in descending
order.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
to return the bottom three documents
with the lowest
<code class="leafygreen-ui-1l06pbn">
score
</code>
for each game with
<code class="leafygreen-ui-1l06pbn">
n: 3
</code>
.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: \"G1\",\n playerId: [ [ \"PlayerB\", 33 ], [ \"PlayerA\", 31 ], [ \"PlayerD\", 1 ] ]\n },\n {\n _id: \"G2\",\n playerId: [ [ \"PlayerC\", 66 ], [ \"PlayerB\", 14 ], [ \"PlayerA\", 10 ] ]\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"G1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">playerId</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"PlayerB"</span>, <span class="lg-highlight-number">33</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-number">31</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerD"</span>, <span class="lg-highlight-number">1</span> ] ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"G2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">playerId</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"PlayerC"</span>, <span class="lg-highlight-number">66</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerB"</span>, <span class="lg-highlight-number">14</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"PlayerA"</span>, <span class="lg-highlight-number">10</span> ] ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The SQL equivalent to this query is:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT PLAYERID,GAMEID,SCORE\nFROM(\n SELECT ROW_NUMBER() OVER (PARTITION BY GAMEID ORDER BY SCORE DESC) AS GAMERANK,\n GAMEID,PLAYERID,SCORE\n FROM GAMESCORES\n) AS T\nWHERE GAMERANK >= 2\nORDER BY GAMEID","programmingLanguage":"SQL"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light sql leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">SELECT</span> PLAYERID,GAMEID,SCORE</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-built_in">ROW_NUMBER</span>(<!-- -->) <span class="lg-highlight-keyword">OVER</span> (<span class="lg-highlight-keyword">PARTITION</span> <span class="lg-highlight-keyword">BY</span> GAMEID <span class="lg-highlight-keyword">ORDER</span> <span class="lg-highlight-keyword">BY</span> SCORE <span class="lg-highlight-keyword">DESC</span>) <span class="lg-highlight-keyword">AS</span> GAMERANK,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> GAMEID,PLAYERID,SCORE</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> GAMESCORES</td></tr><tr class=""><td class="leafygreen-ui-7razhx">) <span class="lg-highlight-keyword">AS</span> T</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> GAMERANK <span class="lg-highlight-operator">>=</span> <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">ORDER</span> <span class="lg-highlight-keyword">BY</span> GAMEID</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Computing
<code class="leafygreen-ui-1l06pbn">
n
</code>
Based on the Group Key for
<code class="leafygreen-ui-1l06pbn">
$group
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#computing-n-based-on-the-group-key-for--group" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="computing-n-based-on-the-group-key-for--group">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can also assign the value of
<code class="leafygreen-ui-1l06pbn">
n
</code>
dynamically. In this example,
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/cond/#mongodb-expression-exp.-cond">
<code class="leafygreen-ui-1l06pbn">
$cond
</code>
</a>
expression is used on the
<code class="leafygreen-ui-1l06pbn">
gameId
</code>
field.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.gamescores.aggregate([\n {\n $group:\n {\n _id: {\"gameId\": \"$gameId\"},\n gamescores:\n {\n $bottomN:\n {\n output: \"$score\",\n n: { $cond: { if: {$eq: [\"$gameId\",\"G2\"] }, then: 1, else: 3 } },\n sortBy: { \"score\": -1 }\n }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">gamescores</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$group</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <!-- -->{<span class="lg-highlight-string">"gameId"</span>: <span class="lg-highlight-string">"$gameId"</span>}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">gamescores</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$bottomN</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">output</span>: <span class="lg-highlight-string">"$score"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>: <!-- -->{ <span class="lg-highlight-attr">$cond</span>: <!-- -->{ <span class="lg-highlight-attr">if</span>: <!-- -->{<span class="lg-highlight-attr">$eq</span>: <!-- -->[<span class="lg-highlight-string">"$gameId"</span>,<span class="lg-highlight-string">"G2"</span>] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">else</span>: <span class="lg-highlight-number">3</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-string">"score"</span>: <!-- -->-<span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The example pipeline:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use
<code class="leafygreen-ui-1l06pbn">
$group
</code>
to group the results by
<code class="leafygreen-ui-1l06pbn">
gameId
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Specifies the fields that are output from
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
with
<code class="leafygreen-ui-1l06pbn">
output : "$score"
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
gameId
</code>
is
<code class="leafygreen-ui-1l06pbn">
G2
</code>
then
<code class="leafygreen-ui-1l06pbn">
n
</code>
is 1, otherwise
<code class="leafygreen-ui-1l06pbn">
n
</code>
is 3.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses sort by
<code class="leafygreen-ui-1l06pbn">
{ "score": -1 }
</code>
to sort the results in descending
order.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n { _id: { gameId: \"G2\" }, gamescores: [ 10 ] },\n { _id: { gameId: \"G1\" }, gamescores: [ 33, 31, 1 ] }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <!-- -->{ <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G2"</span> }<!-- -->, <span class="lg-highlight-attr">gamescores</span>: <!-- -->[ <span class="lg-highlight-number">10</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <!-- -->{ <span class="lg-highlight-attr">gameId</span>: <span class="lg-highlight-string">"G1"</span> }<!-- -->, <span class="lg-highlight-attr">gamescores</span>: <!-- -->[ <span class="lg-highlight-number">33</span>, <span class="lg-highlight-number">31</span>, <span class="lg-highlight-number">1</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.collection.initializeOrderedBulkOp/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.collection.initializeOrderedBulkOp()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.collection.initializeorderedbulkop--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="db.collection.initializeorderedbulkop--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
MongoDB also provides the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.bulkWrite/#mongodb-method-db.collection.bulkWrite">
<code class="leafygreen-ui-1l06pbn">
db.collection.bulkWrite()
</code>
</a>
method for
performing bulk write operations.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.collection.initializeOrderedBulkOp()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.collection.initializeOrderedBulkOp" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-db.collection.initializeOrderedBulkOp">
</div>
</a>
</dt>
<dd>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for a language-specific driver, such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Initializes and returns a new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk/#mongodb-method-Bulk">
<code class="leafygreen-ui-1l06pbn">
Bulk()
</code>
</a>
operations builder
for a collection. The builder constructs an ordered list of write
operations that MongoDB executes in bulk.
</p>
<table class="css-11h39yh e1nex8bq0">
<colgroup>
<col class="field-name"/>
<col class="field-body"/>
</colgroup>
<tbody>
<tr class="css-x8snli">
<th>
Returns
<!-- -->
:
</th>
<td>
new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk/#mongodb-method-Bulk">
<code class="leafygreen-ui-1l06pbn">
Bulk()
</code>
</a>
operations builder object.
</td>
</tr>
</tbody>
</table>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Order of Operation
<a class="headerlink leafygreen-ui-1kepx47" href="#order-of-operation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="order-of-operation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
With an
<em>
ordered
</em>
operations list, MongoDB executes the write
operations in the list serially.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Execution of Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#execution-of-operations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="execution-of-operations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When executing an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.initializeOrderedBulkOp" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ordered
</code>
</span>
</a>
list of operations, MongoDB
groups the operations by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.getOperations/#mongodb-data-batchType">
<code class="leafygreen-ui-1l06pbn">
operation type
</code>
</a>
and
contiguity; i.e.
<em>
contiguous
</em>
operations of the same type are grouped
together. For example, if an ordered list has two insert operations
followed by an update operation followed by another insert operation,
MongoDB groups the operations into three separate groups: first group
contains the two insert operations, second group contains the update
operation, and the third group contains the last insert operation. This
behavior is subject to change in future versions.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk/#mongodb-method-Bulk">
<code class="leafygreen-ui-1l06pbn">
Bulk()
</code>
</a>
operations in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
and comparable
methods in the drivers do not have a limit for the number of operations
in a group. To see how the operations are grouped for bulk operation
execution, call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.getOperations/#mongodb-method-Bulk.getOperations">
<code class="leafygreen-ui-1l06pbn">
Bulk.getOperations()
</code>
</a>
<em>
after
</em>
the execution.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-Write-Command-Batch-Limit-Size">
Write Command Batch Limit Size
</a>
</p>
</li>
</ul>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Executing an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.initializeOrderedBulkOp" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ordered
</code>
</span>
</a>
list of operations on a
sharded collection will generally be slower than executing an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.initializeUnorderedBulkOp/#mongodb-method-db.collection.initializeUnorderedBulkOp">
<code class="leafygreen-ui-1l06pbn">
unordered
</code>
</a>
list
since with an ordered list, each operation must wait for the previous
operation to finish.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Error Handling
<a class="headerlink leafygreen-ui-1kepx47" href="#error-handling" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="error-handling">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If an error occurs during the processing of one of the write
operations, MongoDB will return without processing any remaining write
operations in the list.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following initializes a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk/#mongodb-method-Bulk">
<code class="leafygreen-ui-1l06pbn">
Bulk()
</code>
</a>
operations builder on the
<code class="leafygreen-ui-1l06pbn">
users
</code>
collection, adds a series of write operations, and executes
the operations:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var bulk = db.users.initializeOrderedBulkOp();\nbulk.insert( { user: \"abc123\", status: \"A\", points: 0 } );\nbulk.insert( { user: \"ijk123\", status: \"A\", points: 0 } );\nbulk.insert( { user: \"mop123\", status: \"P\", points: 0 } );\nbulk.find( { status: \"D\" } ).delete();\nbulk.find( { status: \"P\" } ).update( { $set: { comment: \"Pending\" } } );\nbulk.execute();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> bulk <!-- -->= <!-- -->db.<span class="lg-highlight-property">users</span>.<span class="lg-highlight-title lg-highlight-function">initializeOrderedBulkOp</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">insert</span>( <!-- -->{ <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">"abc123"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">0</span> } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">insert</span>( <!-- -->{ <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">"ijk123"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">0</span> } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">insert</span>( <!-- -->{ <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">"mop123"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"P"</span>, <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">0</span> } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"D"</span> } ).<span class="lg-highlight-title lg-highlight-function">delete</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"P"</span> } ).<span class="lg-highlight-title lg-highlight-function">update</span>( <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">"Pending"</span> } } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">execute</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.initializeUnorderedBulkOp/#mongodb-method-db.collection.initializeUnorderedBulkOp">
<code class="leafygreen-ui-1l06pbn">
db.collection.initializeUnorderedBulkOp()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find/#mongodb-method-Bulk.find">
<code class="leafygreen-ui-1l06pbn">
Bulk.find()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.removeOne/#mongodb-method-Bulk.find.removeOne">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.removeOne()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.execute/#mongodb-method-Bulk.execute">
<code class="leafygreen-ui-1l06pbn">
Bulk.execute()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/dropConnections/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
dropConnections
<a class="headerlink leafygreen-ui-1kepx47" href="#dropconnections" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="dropconnections">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
<span>
Command Fields
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#access-control" target="_self">
<span>
Access Control
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.dropConnections" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.dropConnections">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropConnections" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
</span>
</a>
command drops the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
/
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance's outgoing
connections to the specified hosts. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropConnections" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
</span>
</a>
must be run against the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand(\n {\n dropConnections: 1,\n hostAndPort : [ \"host1:port1\", \"host2:port2\", ... ],\n comment: <any>\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">dropConnections</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->hostAndPort <!-- -->: <!-- -->[ <span class="lg-highlight-string">"host1:port1"</span>, <span class="lg-highlight-string">"host2:port2"</span>, <!-- -->...<!-- --> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <<!-- -->any<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command requires the following field:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:10%"/>
<col style="width:70%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
hostAndPort
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Each array element represents the hostname and
port of a remote machine.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
comment
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
any
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. A user-provided comment to attach to this command. Once set, this
comment appears alongside records of this command in the following
locations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-ref">
mongod log messages
</a>
, in the
<code class="leafygreen-ui-1l06pbn">
attr.command.cursor.comment
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
Database profiler
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#mongodb-data-system.profile.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
<code class="leafygreen-ui-1l06pbn">
currentOp
</code>
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-data-currentOp.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
A comment can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON type
</a>
(string, integer, object, array, etc).
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Access Control
<a class="headerlink leafygreen-ui-1kepx47" href="#access-control" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="access-control">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If the deployment enforces
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authentication/#std-label-authentication">
authentication/authorization
</a>
,
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropConnections" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
</span>
</a>
command requires the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-dropConnections">
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
</a>
action on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-cluster">
cluster
</a>
resource.
</p>
<p class="leafygreen-ui-1kp3ins">
Create a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-user-defined-roles/#std-label-user-defined-roles">
user-defined role
</a>
in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database where the
<code class="leafygreen-ui-1l06pbn">
privilege
</code>
array includes the following document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"resource\" : { \"cluster\" : true } }, \"actions\" : [ \"dropConnections\" ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"resource"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"cluster"</span> <!-- -->: <span class="lg-highlight-literal">true</span> } }<!-- -->, <span class="lg-highlight-string">"actions"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"dropConnections"</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.createUser/#mongodb-method-db.createUser">
<code class="leafygreen-ui-1l06pbn">
db.createUser()
</code>
</a>
to create a user on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database with the custom role.
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
or
</em>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.grantRolesToUser/#mongodb-method-db.grantRolesToUser">
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToUser()
</code>
</a>
to grant the role to an existing
user on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, the following operation creates a
user-defined role on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database with the privileges
to support
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropConnections" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"admin\").createRole(\n {\n \"role\" : \"dropConnectionsRole\",\n \"privileges\" : [\n {\n \"resource\" : { \"cluster\" : true },\n \"actions\" : [ \"dropConnections\" ]\n }\n ],\n \"roles\" : []\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"admin"</span>).<span class="lg-highlight-title lg-highlight-function">createRole</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"dropConnectionsRole"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"privileges"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resource"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"cluster"</span> <!-- -->: <span class="lg-highlight-literal">true</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"actions"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"dropConnections"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"roles"</span> <!-- -->: <!-- -->[<!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Assign the custom role to a user on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"admin\").createUser(\n {\n \"user\" : \"dropConnectionsUser\",\n \"pwd\" : \"replaceThisWithASecurePassword\",\n \"roles\" : [ \"dropConnectionsRole\" ]\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"admin"</span>).<span class="lg-highlight-title lg-highlight-function">createUser</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"user"</span> <!-- -->: <span class="lg-highlight-string">"dropConnectionsUser"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"pwd"</span> <!-- -->: <span class="lg-highlight-string">"replaceThisWithASecurePassword"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"roles"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"dropConnectionsRole"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The created user can execute
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropConnections" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For more examples of user creation, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/create-users/">
Create a User on Self-Managed Deployments
</a>
.
For a tutorial on adding privileges to an existing database user, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-users-and-roles/#std-label-modify-existing-user-access">
Modify Access for an Existing User
<!-- -->
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropConnections" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
</span>
</a>
silently ignores
<code class="leafygreen-ui-1l06pbn">
hostAndPort
</code>
elements
that do not include both the hostname and port of the remote machine.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Consider a replica set with a recently removed member at
<code class="leafygreen-ui-1l06pbn">
oldhost.example.com:27017
</code>
. Running the following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropConnections" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
</span>
</a>
command against each active
replica set member ensures there are no remaining outgoing connections
to
<code class="leafygreen-ui-1l06pbn">
oldhost.example.com:27017
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand(\n {\n \"dropConnections\" : 1,\n \"hostAndPort\" : [\n \"oldhost.example.com:27017\"\n ]\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"dropConnections"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hostAndPort"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"oldhost.example.com:27017"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The command returns output similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"ok\" : 1,\n \"$clusterTime\" : {\n \"clusterTime\" : Timestamp(1551375968, 1),\n \"signature\" : {\n \"hash\" : BinData(0,\"AAAAAAAAAAAAAAAAAAAAAAAAAAA=\"),\n \"keyId\" : NumberLong(0)\n }\n },\n \"operationTime\" : Timestamp(1551375968, 1)\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$clusterTime"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clusterTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1551375968</span>, <span class="lg-highlight-number">1</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"signature"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hash"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">BinData</span>(<span class="lg-highlight-number">0</span>,<span class="lg-highlight-string">"AAAAAAAAAAAAAAAAAAAAAAAAAAA="</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"keyId"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"operationTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1551375968</span>, <span class="lg-highlight-number">1</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You can confirm the status of the connection pool for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/connPoolStats/#mongodb-dbcommand-dbcmd.connPoolStats">
<code class="leafygreen-ui-1l06pbn">
connPoolStats
</code>
</a>
command.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/bulkWrite/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
bulkWrite
<a class="headerlink leafygreen-ui-1kepx47" href="#bulkwrite" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="bulkwrite">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
<span>
Command Fields
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output" target="_self">
<span>
Output
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.bulkWrite" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.bulkWrite">
</div>
</a>
</dt>
<dd>
</dd>
</dl>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, you can use the new
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.bulkWrite" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
</span>
</a>
command to perform many insert, update, and delete operations on
multiple collections in one request. The existing
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.bulkWrite/#mongodb-method-db.collection.bulkWrite">
<code class="leafygreen-ui-1l06pbn">
db.collection.bulkWrite()
</code>
</a>
method only allows you to modify one
collection in one request.
</p>
<p class="leafygreen-ui-1kp3ins">
To specify each collection in the
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command, use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-namespace">
namespace
</a>
(database and collection name).
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has this syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n bulkWrite: 1,\n\n // Include the insert, update, and delete operations\n // in the ops array\n ops: [\n {\n insert: <integer>, // Namespace ID index for insert operation.\n // Must match a namespace ID index in\n // ns specified later in the nsInfo array.\n document: <document>\n },\n {\n update: <integer>, // Namespace ID index for update operation\n filter: <document>,\n updateMods: <document>,\n arrayFilters: [ <filterdocument0>, <filterdocument1>, ... ],\n multi: <bolean>,\n hint: <document>,\n constants: <document>,\n collation: <document>\n },\n {\n delete: <integer>, // Namespace ID index for delete operation\n filter: <document>,\n multi: <boolean>,\n hint: <document>,\n collation: <document>\n },\n ...\n // Additional insert, update, and delete operations in any order\n ...\n ],\n\n // Include the namespaces with collections to modify\n // in the nsInfo array. You can add multiple namespaces here.\n nsInfo: [\n {\n ns: <string>, // Namespace (database and collection name) to modify.\n // Each operation namespace ID index\n // specified in the earlier ops array must\n // match a namespace ID index here.\n collectionUUID: <string>,\n encryptionInformation: <document>\n },\n ...\n // Additional namespaces\n ...\n ],\n\n // Additional fields\n ordered: <boolean>,\n bypassDocumentValidation: <boolean>,\n comment: <string>,\n let: <document>,\n errorsOnly: <boolean>,\n cursor: { batchSize: <integer> },\n writeConcern: <string>\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bulkWrite</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Include the insert, update, and delete operations</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> in the ops </span><span class="lg-highlight-comment">array</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ops</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">insert</span>: <<!-- -->integer<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-comment">// Namespace ID</span><span class="lg-highlight-comment"> index for insert </span><span class="lg-highlight-comment">operation.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Must match a </span><span class="lg-highlight-comment">namespace ID index in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// ns</span><span class="lg-highlight-comment"> specified later in </span><span class="lg-highlight-comment">the nsInfo array.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">document</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">update</span>: <<!-- -->integer<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-comment">// Namespace ID</span><span class="lg-highlight-comment"> index for update </span><span class="lg-highlight-comment">operation</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">updateMods</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">arrayFilters</span>: <!-- -->[ <<!-- -->filterDocument0<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->filterDocument1<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">multi</span>: <<!-- -->bolean<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hint</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">constants</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">collation</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">delete</span>: <<!-- -->integer<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-comment">// Namespace ID</span><span class="lg-highlight-comment"> index for delete </span><span class="lg-highlight-comment">operation</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">multi</span>: <<!-- -->boolean<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hint</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">collation</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Additional insert, update,</span><span class="lg-highlight-comment"> and delete operations </span><span class="lg-highlight-comment">in any order</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Include the namespaces </span><span class="lg-highlight-comment">with collections to modify</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// in the nsInfo</span><span class="lg-highlight-comment"> array. You can </span><span class="lg-highlight-comment">add multiple namespaces here.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nsInfo</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ns</span>: <<!-- -->string<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-comment">// Namespace (database and collection name) to modify.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Each operation namespace </span><span class="lg-highlight-comment">ID index</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> specified in the </span><span class="lg-highlight-comment">earlier ops array must</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> match a namespace </span><span class="lg-highlight-comment">ID index here.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">collectionUUID</span>: <<!-- -->string<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">encryptionInformation</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Additional namespaces</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Additional fields</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ordered</span>: <<!-- -->boolean<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bypassDocumentValidation</span>: <<!-- -->boolean<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <<!-- -->string<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">let</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">errorsOnly</span>: <<!-- -->boolean<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cursor</span>: <!-- -->{ <span class="lg-highlight-attr">batchSize</span>: <<!-- -->integer<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">writeConcern</span>: <<!-- -->string<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the command syntax, you can specify multiple:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Insert, update, and delete operations in any order in the
<code class="leafygreen-ui-1l06pbn">
ops
</code>
array.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Namespaces for the operations in the
<code class="leafygreen-ui-1l06pbn">
nsInfo
</code>
array. To match the
operation to the namespace, use the same namespace ID index. Indexes
start at
<code class="leafygreen-ui-1l06pbn">
0
</code>
. You can use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharding">
sharded
</a>
collections.
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command takes the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:10%"/>
<col style="width:15%"/>
<col style="width:10%"/>
<col style="width:65%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Necessity
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
insert
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Namespace ID index for an insert operation, which must match a
namespace ID index in the
<code class="leafygreen-ui-1l06pbn">
ns
</code>
field in the
<code class="leafygreen-ui-1l06pbn">
nsInfo
</code>
array.
Indexes start at
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
document
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Document to insert into the collection.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
update
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Namespace ID index for an update operation, which must match a
namespace ID index in the
<code class="leafygreen-ui-1l06pbn">
ns
</code>
field in the
<code class="leafygreen-ui-1l06pbn">
nsInfo
</code>
array.
Indexes start at
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
filter
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-selectors">
Query selector
</a>
to limit the documents for
the update or delete operation.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
updateMods
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Update operation to perform on the collection. You can specify one
of these:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A document with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/#std-label-update-operators">
update operator
</a>
expressions.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
aggregation pipeline
</a>
in the
form
<code class="leafygreen-ui-1l06pbn">
[ <stage1>, <stage2>, ... ]
</code>
with stages for the
updates.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Array of filter documents that specify the documents to
modify for an update operation on an array field.
</p>
<p class="leafygreen-ui-1kp3ins">
For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/findAndModify/#std-label-findAndModify-command-arrayFilters">
Array Update Operations with
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
multi
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
multi
</code>
field is
<code class="leafygreen-ui-1l06pbn">
true
</code>
, the update or delete operation
updates or deletes all documents that match the document
<code class="leafygreen-ui-1l06pbn">
filter
</code>
. If
<code class="leafygreen-ui-1l06pbn">
false
</code>
, the operation updates or deletes the first document that
matches the document
<code class="leafygreen-ui-1l06pbn">
filter
</code>
. For details on multi-document
transactions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
Transactions
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Default is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
hint
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/indexes/#std-label-indexes">
Index
</a>
to use for the document
<code class="leafygreen-ui-1l06pbn">
filter
</code>
. If the
index doesn't exist, the update operation returns an error.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
constants
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Constants for an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
aggregation pipeline
</a>
custom update.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
collation
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
Collation
</a>
for an update or delete operation.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
delete
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Namespace ID index for a delete operation, which must match a
namespace ID index in the
<code class="leafygreen-ui-1l06pbn">
ns
</code>
field in the
<code class="leafygreen-ui-1l06pbn">
nsInfo
</code>
array.
Indexes start at
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
ns
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Namespace (database and collection) for the operations. Set the
namespace ID index for each operation in
<code class="leafygreen-ui-1l06pbn">
ops
</code>
to the matching
namespace array index in
<code class="leafygreen-ui-1l06pbn">
ns
</code>
. Indexes start at
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
collectionUUID
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-vm4wms" tabindex="0">
UUID
</span>
hexadecimal value
that specifies the collection for the operations.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
encryptionInformation
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Encryption information schema and tokens for the operation. For
details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/fundamentals/create-schema/#std-label-csfle-fundamentals-create-schema">
Encryption Schemas
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
ordered
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
true
</code>
, perform ordered operations. Otherwise, perform
unordered operations.
</p>
<p class="leafygreen-ui-1kp3ins">
Ordered operations run in series. If an error occurs, any
remaining operations are cancelled.
</p>
<p class="leafygreen-ui-1kp3ins">
Unordered operations run in parallel. If an error occurs, any
remaining statements are run. The operations may be reordered by
the server to increase performance. Therefore, your applications
should not depend on the order of operation execution.
</p>
<p class="leafygreen-ui-1kp3ins">
Default is
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
bypassDocumentValidation
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
true
</code>
, the operation bypasses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/schema-validation/">
document validation
rules
</a>
. If
<code class="leafygreen-ui-1l06pbn">
false
</code>
, the documents must
be valid.
</p>
<p class="leafygreen-ui-1kp3ins">
Default is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
comment
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. A user-provided comment to attach to this command. Once set, this
comment appears alongside records of this command in the following
locations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-ref">
mongod log messages
</a>
, in the
<code class="leafygreen-ui-1l06pbn">
attr.command.cursor.comment
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
Database profiler
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#mongodb-data-system.profile.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
<code class="leafygreen-ui-1l06pbn">
currentOp
</code>
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-data-currentOp.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
A comment can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON type
</a>
(string, integer, object, array, etc).
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
let
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Document with a list of constants to reference in the operation.
For
<code class="leafygreen-ui-1l06pbn">
let
</code>
examples, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/update/#std-label-update-variables-example">
Use Variables in
<code class="leafygreen-ui-1l06pbn">
let
</code>
Option or
<code class="leafygreen-ui-1l06pbn">
c
</code>
Field
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/delete/#std-label-delete-let-example">
Use Variables in
<code class="leafygreen-ui-1l06pbn">
let
</code>
.
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
errorsOnly
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
true
</code>
, the operation only returns errors and omits
other output.
</p>
<p class="leafygreen-ui-1kp3ins">
Default is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor batchSize
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-cursor">
Cursor
</a>
batch size for the
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command's
returned results. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.batchSize/#mongodb-method-cursor.batchSize">
<code class="leafygreen-ui-1l06pbn">
cursor.batchSize()
</code>
.
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
writeConcern
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
Write concern
</a>
for the operation. Omit to
use the server default.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-bulkWrite-output">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Output
<a class="headerlink leafygreen-ui-1kepx47" href="#output" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="output">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command returns a document with these fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:25%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Cursor with the command results.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.id
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Cursor identifier.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Results of the operations.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch.ok
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
1
</code>
indicates the operation was successful. Otherwise,
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch.idx
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Operation index number, which corresponds to the operation in the
<code class="leafygreen-ui-1l06pbn">
ops
</code>
array. The first operation has an
<code class="leafygreen-ui-1l06pbn">
idx
</code>
value of
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch.code
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Code number for an error.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch.errmsg
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Description for an error.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch.keyPattern
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Document index key specification for an error.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch.keyValue
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Document index key value for an error.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch.n
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Total number of documents affected by an operation.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cursor.firstBatch.nModified
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of documents modified by an update operation.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
nErrors
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of errors for the
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
nInserted
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of inserted documents.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
nMatched
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of matched documents.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
nModified
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of modified documents.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
nUpserted
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of upserted documents.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
nDeleted
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of deleted documents.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
ok
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
1
</code>
indicates the
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command was successful.
Otherwise,
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The output fields may vary depending on the operations you run in the
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command.
</p>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This section describes the
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command behavior.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Multiple Document Field and Retryable Writes
<a class="headerlink leafygreen-ui-1kepx47" href="#multiple-document-field-and-retryable-writes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="multiple-document-field-and-retryable-writes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
multi
</code>
field is
<code class="leafygreen-ui-1l06pbn">
true
</code>
, the update or delete operation
updates or deletes all documents that match the document
<code class="leafygreen-ui-1l06pbn">
filter
</code>
. If
<code class="leafygreen-ui-1l06pbn">
false
</code>
, the operation updates or deletes the first document that
matches the document
<code class="leafygreen-ui-1l06pbn">
filter
</code>
. For details on multi-document
transactions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
Transactions
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To enable retryable writes, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/retryable-writes/#std-label-retryable-writes">
retryable writes
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
You can use
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
insert operations with retryable writes and
the
<code class="leafygreen-ui-1l06pbn">
multi
</code>
field set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
You can use
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
update and delete operations with the
<code class="leafygreen-ui-1l06pbn">
multi
</code>
field set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
. But, you cannot use update or delete
operations with both
<code class="leafygreen-ui-1l06pbn">
multi
</code>
set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
and retryable writes.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Operation Performance
<a class="headerlink leafygreen-ui-1kepx47" href="#operation-performance" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="operation-performance">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If you rewrite existing insert, update, and delete commands as a
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command and set
<code class="leafygreen-ui-1l06pbn">
errorsOnly
</code>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
, the
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command has similar performance as the existing commands.
If you set
<code class="leafygreen-ui-1l06pbn">
errorsOnly
</code>
to
<code class="leafygreen-ui-1l06pbn">
false
</code>
, performance is worse.
</p>
<p class="leafygreen-ui-1kp3ins">
In addition, if you have a sequence of commands like this:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"insert\nupdate\ndelete","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">insert</td></tr><tr class=""><td class="leafygreen-ui-7razhx">update</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">delete</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you replace those commands with the following example fragment, then
the command with the following fragment is faster regardless of other
options:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n bulkWrite: 0,\n ops: [\n insert,\n update,\n delete\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bulkWrite</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ops</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->insert<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->update<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">delete</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Most of the performance improvement is because of network latency, which
is variable depending on your implementation, but the example is always
faster.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This section contains
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command examples.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Single Namespace Bulk Write Example
<a class="headerlink leafygreen-ui-1kepx47" href="#single-namespace-bulk-write-example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="single-namespace-bulk-write-example">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
example modifies a single namespace:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Run:","name":"Create the pizzas example collection"},{"@type":"HowToStep","text":"Run the following bulkWrite command to perform insert, update,\nand delete operations on the pizzas collection:The pizzas collection is in the default test database, so\nthe ns namespace is \"test.pizzas\". The namespace ID index\nis 0, which is set in the first field of the insert, update,\nand delete operations in the ops array.","name":"Modify the pizzas example collection"},{"@type":"HowToStep","text":"The following bulkWrite example output, with various ok: 1\nfields and nErrors: 0, indicates all operations were\nsuccessful:For details about the output fields, see the earlier\nOutput section.","name":"Examine the output"}],"name":"Single Namespace Bulk Write Example","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Create the pizzas example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-pizzas-example-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-the-pizzas-example-collection">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.pizzas.insertMany( [\n { _id: 0, type: \"pepperoni\", size: \"small\", price: 4 },\n { _id: 1, type: \"cheese\", size: \"medium\", price: 7 },\n { _id: 2, type: \"vegan\", size: \"large\", price: 8 }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">pizzas</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"pepperoni"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"cheese"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">7</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"vegan"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"large"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">8</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Modify the pizzas example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#modify-the-pizzas-example-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="modify-the-pizzas-example-collection">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Run the following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command to perform insert, update,
and delete operations on the
<code class="leafygreen-ui-1l06pbn">
pizzas
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n bulkWrite: 1,\n\n // The ops array contains the insert, update, and delete\n // operations.\n ops: [\n\n // Specify the namespace ID index immediately after\n // the insert, update, and delete text.\n // For example, \"insert: 0\" specifies the 0 namespace ID index,\n // which is the \"test.pizzas\" namespace in nsInfo at the end\n // of the example.\n\n // Insert a pizza.\n { insert: 0, document: { _id: 4, type: \"sausage\",\n size: \"small\", price: 12 } },\n\n // Update the price for medium pizzas.\n { update: 0, filter: { size: \"medium\" },\n updateMods: { $set: { price: 15 } } },\n\n // Delete the pizza with an _id of 2.\n { delete: 0, filter: { _id: 2 } }\n ],\n\n // The nsInfo array contains the namespace to apply the\n // previous operations to.\n nsInfo: [\n { ns: \"test.pizzas\" } // Namespace ID index is 0.\n ]\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bulkWrite</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> The ops array </span><span class="lg-highlight-comment">contains the insert, update, and delete</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// operations.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ops</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Specify the namespace </span><span class="lg-highlight-comment">ID index immediately after</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// the insert, update, and delete text.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// For example, "insert: 0" specifies the 0 namespace ID index,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> which is the </span><span class="lg-highlight-comment">"test.pizzas" namespace in nsInfo at the end</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// of the example.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Insert a pizza.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"sausage"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Update the price </span><span class="lg-highlight-comment">for medium pizzas.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">update</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">updateMods</span>: <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">15</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Delete the pizza </span><span class="lg-highlight-comment">with an _id of 2.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">delete</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// The nsInfo</span><span class="lg-highlight-comment"> array contains the </span><span class="lg-highlight-comment">namespace to apply the</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// previous operations to.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nsInfo</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">"test.pizzas"</span> } <span class="lg-highlight-comment">// Namespace ID index is 0.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
pizzas
</code>
collection is in the default
<code class="leafygreen-ui-1l06pbn">
test
</code>
database, so
the
<code class="leafygreen-ui-1l06pbn">
ns
</code>
namespace is
<code class="leafygreen-ui-1l06pbn">
"test.pizzas"
</code>
. The namespace ID index
is
<code class="leafygreen-ui-1l06pbn">
0
</code>
, which is set in the first field of the insert, update,
and delete operations in the
<code class="leafygreen-ui-1l06pbn">
ops
</code>
array.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Examine the output
<a class="headerlink leafygreen-ui-1kepx47" href="#examine-the-output" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examine-the-output">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
example output, with various
<code class="leafygreen-ui-1l06pbn">
ok: 1
</code>
fields and
<code class="leafygreen-ui-1l06pbn">
nErrors: 0
</code>
, indicates all operations were
successful:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n cursor: {\n id: Long('0'),\n firstBatch: [\n { ok: 1, idx: 0, n: 1 },\n { ok: 1, idx: 1, n: 1, nModified: 1 },\n { ok: 1, idx: 2, n: 1 }\n ],\n ns: 'admin.$cmd.bulkWrite'\n },\n nErrors: 0,\n nInserted: 1,\n nMatched: 1,\n nModified: 1,\n nUpserted: 0,\n nDeleted: 1,\n ok: 1\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cursor</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">id</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">'0'</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">firstBatch</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">nModified</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">'admin.$cmd.bulkWrite'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nErrors</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nInserted</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nMatched</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nModified</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nUpserted</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nDeleted</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For details about the output fields, see the earlier
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-bulkWrite-output" target="_self">
<span>
Output
</span>
</a>
section.
</p>
</section>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Multiple Namespaces Bulk Write Example
<a class="headerlink leafygreen-ui-1kepx47" href="#multiple-namespaces-bulk-write-example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="multiple-namespaces-bulk-write-example">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can specify multiple namespaces in a
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
example contains insert, update, and delete
operations for two namespaces:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"If you already have the pizzas collection in your test\ndatabase, use the db.collection.drop() method to drop it\nfirst and then run:","name":"Create the pizzas example collection"},{"@type":"HowToStep","text":"Run:","name":"Create the pizza orders example collection"},{"@type":"HowToStep","text":"Run the following bulkWrite command to perform insert, update,\nand delete operations on the example collections:","name":"Modify the example collections"},{"@type":"HowToStep","text":"The following bulkWrite example output indicates the\noperations were successful:","name":"Examine the output"}],"name":"Multiple Namespaces Bulk Write Example","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Create the pizzas example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-pizzas-example-collection-1" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-the-pizzas-example-collection-1">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
If you already have the
<code class="leafygreen-ui-1l06pbn">
pizzas
</code>
collection in your
<code class="leafygreen-ui-1l06pbn">
test
</code>
database, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.drop/#mongodb-method-db.collection.drop">
<code class="leafygreen-ui-1l06pbn">
db.collection.drop()
</code>
</a>
method to drop it
first and then run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.pizzas.insertMany( [\n { _id: 0, type: \"pepperoni\", size: \"small\", price: 4 },\n { _id: 1, type: \"cheese\", size: \"medium\", price: 7 },\n { _id: 2, type: \"vegan\", size: \"large\", price: 8 }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">pizzas</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"pepperoni"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"cheese"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">7</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"vegan"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"large"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">8</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Create the pizza orders example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-pizza-orders-example-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-the-pizza-orders-example-collection">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.pizzaOrders.insertMany( [\n { _id: 0, type: \"pepperoni\", number: 5,\n orderDate: new Date( \"2023-01-15T12:00:00Z\" ) },\n { _id: 1, type: \"cheese\", number: 15,\n orderDate: new Date( \"2023-01-23T11:12:32Z\" ) },\n { _id: 2, type: \"vegan\", number: 20,\n orderDate: new Date( \"2023-03-20T10:01:12Z\" ) }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">pizzaOrders</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"pepperoni"</span>, <span class="lg-highlight-attr">number</span>: <span class="lg-highlight-number">5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2023-01-15T12:00:00Z"</span> ) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"cheese"</span>, <span class="lg-highlight-attr">number</span>: <span class="lg-highlight-number">15</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2023-01-23T11:12:32Z"</span> ) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"vegan"</span>, <span class="lg-highlight-attr">number</span>: <span class="lg-highlight-number">20</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2023-03-20T10:01:12Z"</span> ) }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Modify the example collections
<a class="headerlink leafygreen-ui-1kepx47" href="#modify-the-example-collections" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="modify-the-example-collections">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Run the following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command to perform insert, update,
and delete operations on the example collections:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n bulkWrite: 1,\n\n // The ops array contains the insert, update, and delete\n // operations.\n ops: [\n\n // Specify the namespace ID indexes immediately after\n // the insert, update, and delete. For example, \"insert: 0\"\n // specifies the 0 namespace ID index, which is the \"test.pizzas\"\n // namespace. And, \"insert: 1\" specifies \"test.pizzaOrders\".\n\n // Insert pizzas.\n // Namespace ID is 0 for \"test.pizzas\", which\n // is specified as \"insert: 0\".\n { insert: 0, document: { _id: 5, type: \"sausage\",\n size: \"small\", price: 12 } },\n { insert: 0, document: { _id: 6, type: \"vegan cheese\",\n size: \"large\", price: 25 } },\n\n // Update the price for cheese pizzas.\n { update: 0, filter: { type: \"cheese\" },\n updateMods: { $set: { price: 15 } } },\n\n // Delete pizzas with a price less than 7.\n { delete: 0, filter: { price: { $lt: 7 } } },\n\n // Insert pizza orders.\n // Namespace ID is 1 for \"test.pizzaOrders\".\n { insert: 1, document: { _id: 3, type: \"sausage\", number: 7,\n orderDate: new Date( \"2023-04-15T12:02:15Z\" ) } },\n { insert: 1, document: { _id: 4, type: \"vegan\", number: 16,\n orderDate: new Date( \"2023-05-12T11:03:11Z\" ) } },\n\n // Update the number of pizza orders for cheese pizzas.\n { update: 1, filter: { type: \"cheese\" },\n updateMods: { $set: { number: 50 } } },\n\n // Delete the pizza order with an _id of 2.\n { delete: 1, filter: { _id: 2 } },\n\n // Delete pizza orders placed before March 15, 2023.\n { delete: 1, filter: { orderDate:\n { $lte: ISODate( \"2023-03-15T00:00:00Z\" ) } } }\n ],\n\n // Namespaces\n nsInfo: [\n { ns: \"test.pizzas\" }, // Namespace ID index is 0.\n { ns: \"test.pizzaOrders\" } // Namespace ID index is 1.\n ]\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bulkWrite</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> The ops array </span><span class="lg-highlight-comment">contains the insert, update, and delete</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// operations.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ops</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Specify the namespace </span><span class="lg-highlight-comment">ID indexes immediately after</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// the insert, update,</span><span class="lg-highlight-comment"> and delete. For </span><span class="lg-highlight-comment">example, "insert: 0"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// specifies the 0 namespace ID index,</span><span class="lg-highlight-comment"> which is the </span><span class="lg-highlight-comment">"test.pizzas"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// namespace. And, "insert: 1" specifies "test.pizzaOrders".</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Insert pizzas.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Namespace ID is 0 for "test.pizzas", which</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// is specified as "insert: 0".</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"sausage"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"vegan cheese"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"large"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">25</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Update the price </span><span class="lg-highlight-comment">for cheese pizzas.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">update</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"cheese"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">updateMods</span>: <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">15</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Delete pizzas with </span><span class="lg-highlight-comment">a</span><span class="lg-highlight-comment"> price less than </span><span class="lg-highlight-comment">7.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">delete</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <!-- -->{ <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-number">7</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Insert pizza orders.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Namespace ID is 1 for "test.pizzaOrders".</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"sausage"</span>, <span class="lg-highlight-attr">number</span>: <span class="lg-highlight-number">7</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2023-04-15T12:02:15Z"</span> ) } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"vegan"</span>, <span class="lg-highlight-attr">number</span>: <span class="lg-highlight-number">16</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2023-05-12T11:03:11Z"</span> ) } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Update the number </span><span class="lg-highlight-comment">of</span><span class="lg-highlight-comment"> pizza orders for </span><span class="lg-highlight-comment">cheese pizzas.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">update</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"cheese"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">updateMods</span>: <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">number</span>: <span class="lg-highlight-number">50</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Delete the pizza </span><span class="lg-highlight-comment">order with an _id of 2.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">delete</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Delete pizza orders </span><span class="lg-highlight-comment">placed before March 15, 2023.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">delete</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">orderDate</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$lte</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-03-15T00:00:00Z"</span> ) } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Namespaces</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nsInfo</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">"test.pizzas"</span> }<!-- -->, <span class="lg-highlight-comment">// Namespace ID index is 0.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">"test.pizzaOrders"</span> } <span class="lg-highlight-comment">// Namespace ID index is 1.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
4
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Examine the output
<a class="headerlink leafygreen-ui-1kepx47" href="#examine-the-output-1" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examine-the-output-1">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
example output indicates the
operations were successful:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n cursor: {\n id: Long('0'),\n firstBatch: [\n { ok: 1, idx: 0, n: 1 },\n { ok: 1, idx: 1, n: 1 },\n { ok: 1, idx: 2, n: 1, nModified: 1 },\n { ok: 1, idx: 3, n: 1 },\n { ok: 1, idx: 4, n: 1 },\n { ok: 1, idx: 5, n: 1 },\n { ok: 1, idx: 6, n: 1, nModified: 1 },\n { ok: 1, idx: 7, n: 1 },\n { ok: 1, idx: 8, n: 1 }\n ],\n ns: 'admin.$cmd.bulkWrite'\n },\n nErrors: 0,\n nInserted: 4,\n nMatched: 2,\n nModified: 2,\n nUpserted: 0,\n nDeleted: 3,\n ok: 1\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cursor</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">id</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">'0'</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">firstBatch</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">nModified</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">nModified</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">'admin.$cmd.bulkWrite'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nErrors</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nInserted</span>: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nMatched</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nModified</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nUpserted</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nDeleted</span>: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Operations with Errors Bulk Write Example
<a class="headerlink leafygreen-ui-1kepx47" href="#operations-with-errors-bulk-write-example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="operations-with-errors-bulk-write-example">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
example contains operations with errors and
operations that don't change any documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"If you already have the pizzas collection in your test\ndatabase, use the db.collection.drop() method to drop it\nfirst and then run:","name":"Create the pizzas example collection"},{"@type":"HowToStep","text":"Run the following bulkWrite command to perform insert, update,\nand delete operations on the pizzas collection:","name":"Attempt to modify the pizzas example collection"},{"@type":"HowToStep","text":"The following bulkWrite example output shows the errors:For details about the output fields, including the error codes and\nmessages, see the earlier Output section.","name":"Examine the output"}],"name":"Operations with Errors Bulk Write Example","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Create the pizzas example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-pizzas-example-collection-2" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-the-pizzas-example-collection-2">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
If you already have the
<code class="leafygreen-ui-1l06pbn">
pizzas
</code>
collection in your
<code class="leafygreen-ui-1l06pbn">
test
</code>
database, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.drop/#mongodb-method-db.collection.drop">
<code class="leafygreen-ui-1l06pbn">
db.collection.drop()
</code>
</a>
method to drop it
first and then run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.pizzas.insertMany( [\n { _id: 0, type: \"pepperoni\", size: \"small\", price: 4 },\n { _id: 1, type: \"cheese\", size: \"medium\", price: 7 },\n { _id: 2, type: \"vegan\", size: \"large\", price: 8 }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">pizzas</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"pepperoni"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"cheese"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">7</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"vegan"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"large"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">8</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Attempt to modify the pizzas example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#attempt-to-modify-the-pizzas-example-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="attempt-to-modify-the-pizzas-example-collection">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Run the following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command to perform insert, update,
and delete operations on the
<code class="leafygreen-ui-1l06pbn">
pizzas
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n bulkWrite: 1,\n\n // The ops array contains the insert, update, and delete\n // operations.\n ops: [\n\n // The namespace ID indexes are specified immediately after\n // the insert, update, and delete text.\n // For example, \"insert: 0\" specifies the 0 namespace ID index,\n // which is the \"test.pizzas\" namespace in nsInfo.\n\n // Attempt to add a duplicate document with an\n // _id of 1, which already exists and causes an error.\n { insert: 0, document: { _id: 1, type: \"tomato\",\n size: \"small\", price: 12 } },\n\n // Attempt to add another duplicate document.\n { insert: 0, document: { _id: 2, type: \"pepper\",\n size: \"small\", price: 12 } },\n\n // Attempt to change the price for extra large pizzas,\n // which don't exist. This doesn't cause an error but\n // doesn't update any documents.\n { update: 0, filter: { size: \"extra large\" },\n updateMods: { $set: { price: 15 } } },\n\n // Attempt to remove a document that doesn't exist.\n // This doesn't cause an error but doesn't delete any documents.\n { delete: 0, filter: { _id: 8 } }\n ],\n\n // The nsInfo array contains the namespace to apply the\n // previous operations to.\n nsInfo: [\n { ns: \"test.pizzas\" } // Namespace ID index is 0.\n ],\n\n // Set the ordered field to false to run the remaining operations\n // after an operation returns an error.\n ordered: false\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bulkWrite</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> The ops array </span><span class="lg-highlight-comment">contains the insert, update, and delete</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// operations.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ops</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// The namespace ID</span><span class="lg-highlight-comment"> indexes are specified </span><span class="lg-highlight-comment">immediately after</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// the insert, update, and delete text.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// For example, "insert: 0" specifies the 0 namespace ID index,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> which is the </span><span class="lg-highlight-comment">"test.pizzas" namespace in nsInfo.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Attempt to add </span><span class="lg-highlight-comment">a</span><span class="lg-highlight-comment"> duplicate document with </span><span class="lg-highlight-comment">an</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// _id of 1,</span><span class="lg-highlight-comment"> which already exists </span><span class="lg-highlight-comment">and causes an error.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"tomato"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Attempt to add </span><span class="lg-highlight-comment">another duplicate document.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"pepper"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Attempt to change </span><span class="lg-highlight-comment">the</span><span class="lg-highlight-comment"> price for extra </span><span class="lg-highlight-comment">large pizzas,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> which don't exist. </span><span class="lg-highlight-comment">This doesn't cause an error but</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> doesn't update any </span><span class="lg-highlight-comment">documents.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">update</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"extra large"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">updateMods</span>: <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">15</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Attempt to remove </span><span class="lg-highlight-comment">a</span><span class="lg-highlight-comment"> document that doesn't </span><span class="lg-highlight-comment">exist.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> This doesn't cause </span><span class="lg-highlight-comment">an</span><span class="lg-highlight-comment"> error but doesn't </span><span class="lg-highlight-comment">delete any documents.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">delete</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">8</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// The nsInfo</span><span class="lg-highlight-comment"> array contains the </span><span class="lg-highlight-comment">namespace to apply the</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// previous operations to.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nsInfo</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">"test.pizzas"</span> } <span class="lg-highlight-comment">// Namespace ID index is 0.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Set the ordered </span><span class="lg-highlight-comment">field</span><span class="lg-highlight-comment"> to false to </span><span class="lg-highlight-comment">run the remaining operations</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// after an operation returns an error.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ordered</span>: <span class="lg-highlight-literal">false</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Examine the output
<a class="headerlink leafygreen-ui-1kepx47" href="#examine-the-output-2" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examine-the-output-2">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
example output shows the errors:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n cursor: {\n id: Long(\"0\"),\n firstBatch: [\n {\n ok: 0,\n idx: 0,\n code: 11000,\n errmsg: 'E11000 duplicate key error collection:\n test.pizzas index: _id_ dup key: { _id: 1 }',\n keyPattern: { _id: 1 },\n keyValue: { _id: 1 },\n n: 0\n },\n {\n ok: 0,\n idx: 1,\n code: 11000,\n errmsg: 'E11000 duplicate key error collection:\n test.pizzas index: _id_ dup key: { _id: 2 }',\n keyPattern: { _id: 1 },\n keyValue: { _id: 2 },\n n: 0\n },\n { ok: 1, idx: 2, n: 0, nModified: 0 },\n { ok: 1, idx: 3, n: 0 }\n ],\n ns: 'admin.$cmd.bulkWrite'\n },\n nErrors: 2,\n nInserted: 0,\n nMatched: 0,\n nModified: 0,\n nUpserted: 0,\n nDeleted: 0,\n ok: 1\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cursor</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">id</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"0"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">firstBatch</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-number">11000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">errmsg</span>: <span class="lg-highlight-string">'E11000 duplicate key error collection:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> test.pizzas index: _id_ dup key: { _id: 1 }'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyPattern</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyValue</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">0</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-number">11000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">errmsg</span>: <span class="lg-highlight-string">'E11000 duplicate key error collection:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> test.pizzas index: _id_ dup key: { _id: 2 }'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyPattern</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyValue</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">0</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">nModified</span>: <span class="lg-highlight-number">0</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">0</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">'admin.$cmd.bulkWrite'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nErrors</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nInserted</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nMatched</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nModified</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nUpserted</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nDeleted</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For details about the output fields, including the error codes and
messages, see the earlier
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-bulkWrite-output" target="_self">
<span>
Output
</span>
</a>
section.
</p>
</section>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Bulk Write Example with errorsOnly Enabled
<a class="headerlink leafygreen-ui-1kepx47" href="#bulk-write-example-with-errorsonly-enabled" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="bulk-write-example-with-errorsonly-enabled">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
example sets
<code class="leafygreen-ui-1l06pbn">
errorsOnly
</code>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
to
only show the error output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"If you already have the pizzas collection in your test\ndatabase, use the db.collection.drop() method to drop it\nfirst and then run:","name":"Create the pizzas example collection"},{"@type":"HowToStep","text":"Run the following bulkWrite command to perform insert, update,\nand delete operations on the pizzas collection with\nerrorsOnly set to true:","name":"Attempt to modify the pizzas example collection"},{"@type":"HowToStep","text":"The following bulkWrite example output shows the errors:","name":"Examine the output"}],"name":"Bulk Write Example with errorsOnly Enabled","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Create the pizzas example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-pizzas-example-collection-3" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-the-pizzas-example-collection-3">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
If you already have the
<code class="leafygreen-ui-1l06pbn">
pizzas
</code>
collection in your
<code class="leafygreen-ui-1l06pbn">
test
</code>
database, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.drop/#mongodb-method-db.collection.drop">
<code class="leafygreen-ui-1l06pbn">
db.collection.drop()
</code>
</a>
method to drop it
first and then run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.pizzas.insertMany( [\n { _id: 0, type: \"pepperoni\", size: \"small\", price: 4 },\n { _id: 1, type: \"cheese\", size: \"medium\", price: 7 },\n { _id: 2, type: \"vegan\", size: \"large\", price: 8 }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">pizzas</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"pepperoni"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"cheese"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"medium"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">7</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"vegan"</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"large"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">8</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Attempt to modify the pizzas example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#attempt-to-modify-the-pizzas-example-collection-1" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="attempt-to-modify-the-pizzas-example-collection-1">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Run the following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
command to perform insert, update,
and delete operations on the
<code class="leafygreen-ui-1l06pbn">
pizzas
</code>
collection with
<code class="leafygreen-ui-1l06pbn">
errorsOnly
</code>
set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n bulkWrite: 1,\n\n // The ops array contains the insert, update, and delete\n // operations.\n ops: [\n\n // The namespace ID indexes are specified immediately after\n // the insert, update, and delete text.\n // For example, \"insert: 0\" specifies the 0 namespace ID index,\n // which is the \"test.pizzas\" namespace in nsInfo.\n\n // Attempt to add a duplicate document with an\n // _id of 1, which already exists and causes an error.\n { insert: 0, document: { _id: 1, type: \"tomato\",\n size: \"small\", price: 12 } },\n\n // Attempt to add another duplicate document.\n { insert: 0, document: { _id: 2, type: \"pepper\",\n size: \"small\", price: 12 } },\n\n // Attempt to change the price for extra large pizzas,\n // which don't exist. This doesn't cause an error but\n // doesn't update any documents.\n { update: 0, filter: { size: \"extra large\" },\n updateMods: { $set: { price: 15 } } },\n\n // Attempt to remove a document that doesn't exist.\n // This doesn't cause an error but doesn't delete any documents.\n { delete: 0, filter: { _id: 8 } }\n ],\n\n // The nsInfo array contains the namespace to apply the\n // previous operations to.\n nsInfo: [\n { ns: \"test.pizzas\" } // Namespace ID index is 0.\n ],\n\n // Set the ordered field to false to run the remaining operations\n // after an operation returns an error.\n ordered: false,\n\n // Set the errorsOnly field to true to only output the errors.\n errorsOnly: true\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bulkWrite</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> The ops array </span><span class="lg-highlight-comment">contains the insert, update, and delete</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// operations.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ops</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// The namespace ID</span><span class="lg-highlight-comment"> indexes are specified </span><span class="lg-highlight-comment">immediately after</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// the insert, update, and delete text.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// For example, "insert: 0" specifies the 0 namespace ID index,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> which is the </span><span class="lg-highlight-comment">"test.pizzas" namespace in nsInfo.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Attempt to add </span><span class="lg-highlight-comment">a</span><span class="lg-highlight-comment"> duplicate document with </span><span class="lg-highlight-comment">an</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// _id of 1,</span><span class="lg-highlight-comment"> which already exists </span><span class="lg-highlight-comment">and causes an error.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"tomato"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Attempt to add </span><span class="lg-highlight-comment">another duplicate document.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">insert</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">document</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"pepper"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Attempt to change </span><span class="lg-highlight-comment">the</span><span class="lg-highlight-comment"> price for extra </span><span class="lg-highlight-comment">large pizzas,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> which don't exist. </span><span class="lg-highlight-comment">This doesn't cause an error but</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> doesn't update any </span><span class="lg-highlight-comment">documents.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">update</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"extra large"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">updateMods</span>: <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">15</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Attempt to remove </span><span class="lg-highlight-comment">a</span><span class="lg-highlight-comment"> document that doesn't </span><span class="lg-highlight-comment">exist.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> This doesn't cause </span><span class="lg-highlight-comment">an</span><span class="lg-highlight-comment"> error but doesn't </span><span class="lg-highlight-comment">delete any documents.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">delete</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">8</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// The nsInfo</span><span class="lg-highlight-comment"> array contains the </span><span class="lg-highlight-comment">namespace to apply the</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// previous operations to.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nsInfo</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">"test.pizzas"</span> } <span class="lg-highlight-comment">// Namespace ID index is 0.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Set the ordered </span><span class="lg-highlight-comment">field</span><span class="lg-highlight-comment"> to false to </span><span class="lg-highlight-comment">run the remaining operations</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// after an operation returns an error.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ordered</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Set the errorsOnly</span><span class="lg-highlight-comment"> field to true </span><span class="lg-highlight-comment">to</span><span class="lg-highlight-comment"> only output the </span><span class="lg-highlight-comment">errors.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">errorsOnly</span>: <span class="lg-highlight-literal">true</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Examine the output
<a class="headerlink leafygreen-ui-1kepx47" href="#examine-the-output-3" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examine-the-output-3">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
example output shows the errors:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n cursor: {\n id: Long(\"0\"),\n firstBatch: [\n {\n ok: 0,\n idx: 0,\n code: 11000,\n errmsg: 'E11000 duplicate key error collection:\n test.pizzas index: _id_ dup key: { _id: 1 }',\n keyPattern: { _id: 1 },\n keyValue: { _id: 1 },\n n: 0\n },\n {\n ok: 0,\n idx: 1,\n code: 11000,\n errmsg: 'E11000 duplicate key error collection:\n test.pizzas index: _id_ dup key: { _id: 2 }',\n keyPattern: { _id: 1 },\n keyValue: { _id: 2 },\n n: 0\n },\n ],\n ns: 'admin.$cmd.bulkWrite'\n },\n nErrors: 2,\n nInserted: 0,\n nMatched: 0,\n nModified: 0,\n nUpserted: 0,\n nDeleted: 0,\n ok: 1\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cursor</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">id</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"0"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">firstBatch</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-number">11000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">errmsg</span>: <span class="lg-highlight-string">'E11000 duplicate key error collection:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> test.pizzas index: _id_ dup key: { _id: 1 }'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyPattern</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyValue</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">0</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">idx</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-number">11000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">errmsg</span>: <span class="lg-highlight-string">'E11000 duplicate key error collection:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> test.pizzas index: _id_ dup key: { _id: 2 }'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyPattern</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyValue</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">n</span>: <span class="lg-highlight-number">0</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ns</span>: <span class="lg-highlight-string">'admin.$cmd.bulkWrite'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nErrors</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nInserted</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nMatched</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nModified</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nUpserted</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">nDeleted</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/server-sessions/#std-label-server-sessions">
Server Sessions
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-selectors">
Query Selectors
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
Aggregation Pipeline
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/indexes/#std-label-indexes">
Indexes
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
Collation
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/retryable-writes/#std-label-retryable-writes">
Retryable Writes
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
Transactions
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/inconsistency-type/HiddenShardedCollection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
HiddenShardedCollection
<a class="headerlink leafygreen-ui-1kepx47" href="#hiddenshardedcollection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="hiddenshardedcollection">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#format" target="_self">
<span>
Format
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
HiddenShardedCollection
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-HiddenShardedCollection" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-HiddenShardedCollection">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
A collection is registered in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.collections">
<code class="leafygreen-ui-1l06pbn">
config.collections
</code>
</a>
cluster catalog
under a non-existent database.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Format
<a class="headerlink leafygreen-ui-1kepx47" href="#format" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="format">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n type: \"HiddenShardedCollection\",\n description: \"<string>\",\n details: {\n namespace: \"<string>\",\n collection: \"<object>\",\n }\n}","programmingLanguage":"JSON"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"HiddenShardedCollection"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> description<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"<string>"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> details<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> namespace<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"<string>"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> collection<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"<object>"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
HiddenShardedCollection
</code>
inconsistency documents contain the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:25%"/>
<col style="width:45%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
type
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Type of inconsistency document. Returns a value of
<code class="leafygreen-ui-1l06pbn">
HiddenShardedCollection
</code>
for this
inconsistency type.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
description
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Describes the inconsistency found in the metadata.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Document with additional information on where the inconsistency exists in the
database and sharded cluster.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.namespace
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Indicates the database and collection where the inconsistency exists.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.collection
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Information about the collection.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.adminCommand/#mongodb-method-db.adminCommand">
<code class="leafygreen-ui-1l06pbn">
db.adminCommand()
</code>
</a>
method to call the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/checkMetadataConsistency/#mongodb-dbcommand-dbcmd.checkMetadataConsistency">
<code class="leafygreen-ui-1l06pbn">
checkMetadataConsistency
</code>
</a>
command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { checkMetadataConsistency: 1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{ <span class="lg-highlight-attr">checkMetadataConsistency</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The method returns a cursor with a batch of documents showing the inconsistencies
found in the sharding metadata. The example below shows a cursor with
a
<code class="leafygreen-ui-1l06pbn">
HiddenShardedCollection
</code>
inconsistency document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n cursor: {\n id: Long(\"0\"),\n ns: \"test.$cmd.aggregate\",\n firstBatch: [\n {\n type: \"HiddenShardedCollection\",\n description: \"Found sharded collection but relative database does not exist\",\n details: {\n namespace: \"test.authors\",\n collection: {\n _id: \"test.authors\",\n lastmodEpoch: ObjectId(\"64ddd78de906038182671674\"),\n lastmod: ISODate(\"2023-08-17T08:17:17.944Z\"),\n timestamp: Timestamp(1692260237, 58),\n uuid: new UUID(\"69317741-7bc5-4eca-8877-7858cecf67d6\"),\n key: {\n \"skey\" : 1\n },\n unique: false,\n noBalance: false\n }\n }\n }\n ],\n },\n ok: 1\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> id<span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"0"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ns<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.$cmd.aggregate"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> firstBatch<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> type<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"HiddenShardedCollection"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> description<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"Found sharded collection but relative database does not exist"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> details<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> namespace<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.authors"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> collection<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> _id<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.authors"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> lastmodEpoch<span class="lg-highlight-punctuation">:</span> ObjectId(<span class="lg-highlight-string">"64ddd78de906038182671674"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> lastmod<span class="lg-highlight-punctuation">:</span> ISODate(<span class="lg-highlight-string">"2023-08-17T08:17:17.944Z"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> timestamp<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-number">1692260237</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">58</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> uuid<span class="lg-highlight-punctuation">:</span> new UUID(<span class="lg-highlight-string">"69317741-7bc5-4eca-8877-7858cecf67d6"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> key<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"skey"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> unique<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-keyword">false</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> noBalance<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-keyword">false</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">]</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ok<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/type/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$type (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-type--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-type--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$type
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-type" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-type">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns a string that specifies the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON type
</a>
of the argument.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-type" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$type
</code>
</span>
</a>
has the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-agg-quick-ref-operator-expressions">
operator expression
syntax
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $type: <expression> }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$type</span>: <<!-- -->expression<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The argument can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
<!-- -->
.
</a>
</p>
</dd>
</dl>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/isNumber/#mongodb-expression-exp.-isNumber">
<code class="leafygreen-ui-1l06pbn">
$isNumber
</code>
</a>
- checks if the argument is a number.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/type/#mongodb-query-op.-type">
<code class="leafygreen-ui-1l06pbn">
$type (Query)
</code>
</a>
- filters fields based on BSON type.
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$type
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#-type" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-type">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Unlike the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/type/#mongodb-query-op.-type">
<code class="leafygreen-ui-1l06pbn">
$type
</code>
</a>
query operator, which matches array
elements based on their BSON type, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-type" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$type
</code>
</span>
</a>
aggregation operator does not examine array elements. Instead,
when passed an array as its argument, the
<code class="leafygreen-ui-1l06pbn">
$type
</code>
aggregation
operator returns the type of the argument, i.e.
<code class="leafygreen-ui-1l06pbn">
"array"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If the argument is a field that is missing in the input document,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-type" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$type
</code>
</span>
</a>
returns the string
<code class="leafygreen-ui-1l06pbn">
"missing"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The following table shows the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-type" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$type
</code>
</span>
</a>
output for several
common types of expressions:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:80%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Results
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $type: "a" }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"string"
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $type: /a/ }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"regex"
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $type: 1 }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"double"
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $type: NumberLong(627) }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"long"
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $type: { x: 1 } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"object"
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $type: [ [ 1, 2, 3 ] ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"array"
</code>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
In the case of a literal array such as
<code class="leafygreen-ui-1l06pbn">
[ 1, 2, 3 ]
</code>
,
enclose the expression in an outer set
of array brackets to prevent MongoDB from parsing
<code class="leafygreen-ui-1l06pbn">
[ 1, 2, 3 ]
</code>
as an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-agg-quick-ref-operator-expressions">
argument list
</a>
with three arguments (
<code class="leafygreen-ui-1l06pbn">
1, 2, 3
</code>
). Wrapping the array
<code class="leafygreen-ui-1l06pbn">
[ 1, 2, 3 ]
</code>
in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/literal/#mongodb-expression-exp.-literal">
<code class="leafygreen-ui-1l06pbn">
$literal
</code>
</a>
expression
achieves the same result.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-agg-quick-ref-operator-expressions">
operator expression syntax forms
</a>
for more information.
</p>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Available Types
<a class="headerlink leafygreen-ui-1kepx47" href="#available-types" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="available-types">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Number
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Alias
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Notes
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Double
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"double"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
String
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"string"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Object
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
3
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"object"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
4
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"array"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Binary data
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
5
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"binData"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Undefined
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
6
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"undefined"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deprecated.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
ObjectId
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
7
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"objectId"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
8
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"bool"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Date
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
9
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"date"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Null
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
10
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"null"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Regular Expression
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
11
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"regex"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
DBPointer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
12
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"dbPointer"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deprecated.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
JavaScript
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
13
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"javascript"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Symbol
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
14
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"symbol"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deprecated.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
32-bit integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
16
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"int"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Timestamp
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
17
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"timestamp"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
64-bit integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
18
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"long"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Decimal128
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
19
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"decimal"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Min key
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
-1
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"minKey"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Max key
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
127
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"maxKey"
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
If the argument is a field that is missing in the input document,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-type" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$type
</code>
</span>
</a>
returns the string
<code class="leafygreen-ui-1l06pbn">
"missing"
</code>
.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This example uses a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-collection">
collection
</a>
named
<code class="leafygreen-ui-1l06pbn">
coll
</code>
with
the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-document">
documents
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: 0, a : 8 }\n{ _id: 1, a : [ 41.63, 88.19 ] }\n{ _id: 2, a : { a : \"apple\", b : \"banana\", c: \"carrot\" } }\n{ _id: 3, a : \"caribou\" }\n{ _id: 4, a : NumberLong(71) }\n{ _id: 5 }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <!-- -->a <!-- -->: <span class="lg-highlight-number">8</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <!-- -->a <!-- -->: <!-- -->[ <span class="lg-highlight-number">41.63</span>, <span class="lg-highlight-number">88.19</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <!-- -->a <!-- -->: <!-- -->{ <!-- -->a <!-- -->: <span class="lg-highlight-string">"apple"</span>, <!-- -->b <!-- -->: <span class="lg-highlight-string">"banana"</span>, <span class="lg-highlight-attr">c</span>: <span class="lg-highlight-string">"carrot"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <!-- -->a <!-- -->: <span class="lg-highlight-string">"caribou"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <!-- -->a <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">71</span>) }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses the
<code class="leafygreen-ui-1l06pbn">
$type
</code>
operator to display the type of field
<code class="leafygreen-ui-1l06pbn">
a
</code>
for all documents
as part of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
stage.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.coll.aggregate([{\n $project: {\n a : { $type: \"$a\" }\n }\n}])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">coll</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$project</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->a <!-- -->: <!-- -->{ <span class="lg-highlight-attr">$type</span>: <span class="lg-highlight-string">"$a"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: 0, \"a\" : \"double\" }\n{ _id: 1, \"a\" : \"array\" }\n{ _id: 2, \"a\" : \"object\" }\n{ _id: 3, \"a\" : \"string\" }\n{ _id: 4, \"a\" : \"long\" }\n{ _id: 5, \"a\" : \"missing\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">"a"</span> <!-- -->: <span class="lg-highlight-string">"double"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"a"</span> <!-- -->: <span class="lg-highlight-string">"array"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"a"</span> <!-- -->: <span class="lg-highlight-string">"object"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"a"</span> <!-- -->: <span class="lg-highlight-string">"string"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"a"</span> <!-- -->: <span class="lg-highlight-string">"long"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"a"</span> <!-- -->: <span class="lg-highlight-string">"missing"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2dsphere/query/points-within-circle-on-sphere/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Query for Locations within a Circle on a Sphere
<a class="headerlink leafygreen-ui-1kepx47" href="#query-for-locations-within-a-circle-on-a-sphere" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="query-for-locations-within-a-circle-on-a-sphere">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#about-this-task" target="_self">
<span>
About this Task
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#before-you-begin" target="_self">
<span>
Before You Begin
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
You can query for location data within a circle on the surface of a
sphere. Use these queries to return data within a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/w/index.php?title=Spherical_cap&oldid=1107980309" rel="noopener noreferrer">
<span>
spherical cap
<!-- -->
.
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To query for location data within a circle on a sphere, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/geoWithin/#mongodb-query-op.-geoWithin">
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</a>
with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/centerSphere/#mongodb-query-op.-centerSphere">
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
</a>
operator. In the
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
operator, specify the coordinates and radius of the
circle to query within:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.<collection>.find( {\n <location> : {\n $geoWithin : {\n $centerSphere: [\n [ <longitude>, <latitude> ],\n <radius>\n ]\n }\n }\n } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<!-- --><<!-- -->collection<!-- -->><!-- -->.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><location field> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geoWithin : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $centerSphere: [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [ <longitude>, <latitude> ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <radius></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
About this Task
<a class="headerlink leafygreen-ui-1kepx47" href="#about-this-task" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="about-this-task">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When you specify longitude and latitude coordinates, list the
<strong>
longitude
</strong>
first, and then
<strong>
latitude
</strong>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Valid longitude values are between
<code class="leafygreen-ui-1l06pbn">
-180
</code>
and
<code class="leafygreen-ui-1l06pbn">
180
</code>
, both
inclusive.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Valid latitude values are between
<code class="leafygreen-ui-1l06pbn">
-90
</code>
and
<code class="leafygreen-ui-1l06pbn">
90
</code>
, both
inclusive.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In the
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
operator, specify the circle's radius in
<strong>
radians
</strong>
. To convert other units to and from radians,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2d/calculate-distances/#std-label-calculate-distance-spherical-geometry">
Convert Distance to Radians for Spherical Operators
<!-- -->
.
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
This example calculates distance in kilometers. To convert
kilometers to radians, divide the kilometer value by
<code class="leafygreen-ui-1l06pbn">
6378.1
</code>
.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
does not require a geospatial index. However, a
geospatial index improves query performance. Only the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/#std-label-2dsphere-index">
2dsphere
</a>
geospatial index supports
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
. For
more information see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/create/#std-label-create-2dsphere-index">
Create a 2dsphere Index
<!-- -->
.
</a>
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Before You Begin
<a class="headerlink leafygreen-ui-1kepx47" href="#before-you-begin" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="before-you-begin">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
places
</code>
collection that contains these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.insertMany( [\n {\n loc: { type: \"Point\", coordinates: [ -73.97, 40.77 ] },\n name: \"Central Park\",\n category : \"Park\"\n },\n {\n loc: { type: \"Point\", coordinates: [ -73.88, 40.78 ] },\n name: \"La Guardia Airport\",\n category: \"Airport\"\n },\n {\n loc: { type: \"Point\", coordinates: [ -1.83, 51.18 ] },\n name: \"Stonehenge\",\n category : \"Monument\"\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">places</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">73.97</span>, <span class="lg-highlight-number">40.77</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Central Park"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->category <!-- -->: <span class="lg-highlight-string">"Park"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">73.88</span>, <span class="lg-highlight-number">40.78</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"La Guardia Airport"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"Airport"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">1.83</span>, <span class="lg-highlight-number">51.18</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Stonehenge"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->category <!-- -->: <span class="lg-highlight-string">"Monument"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To query the collection, use
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
with the
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
operator:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.find( {\n loc: {\n $geoWithin: {\n $centerSphere: [\n [ -1.76, 51.16 ],\n 10 / 6378.1\n ]\n }\n }\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">places</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geoWithin</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$centerSphere</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <!-- -->-<span class="lg-highlight-number">1.76</span>, <span class="lg-highlight-number">51.16</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-number">10</span> <!-- -->/ <span class="lg-highlight-number">6378.1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The query returns documents where the
<code class="leafygreen-ui-1l06pbn">
loc
</code>
field is within a
10 kilometer radius of a point at longitude
<code class="leafygreen-ui-1l06pbn">
-1.76
</code>
, latitude
<code class="leafygreen-ui-1l06pbn">
51.16
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId(\"63fd205e4a08b5e248c03e32\"),\n loc: { type: 'Point', coordinates: [ -1.83, 51.18 ] },\n name: 'Stonehenge',\n category: 'Monument'\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"63fd205e4a08b5e248c03e32"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'Point'</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">1.83</span>, <span class="lg-highlight-number">51.18</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Stonehenge'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">'Monument'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/geoWithin/#mongodb-query-op.-geoWithin">
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/centerSphere/#mongodb-query-op.-centerSphere">
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/query/geojson-bound-by-polygon/#std-label-2dsphere-query-geojson-objects-polygon">
Query for Locations Bound by a Polygon
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/query/intersections-of-geojson-objects/#std-label-2dsphere-query-intersection">
Query for Locations that Intersect a GeoJSON Object
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/query/proximity-to-geojson/#std-label-2dsphere-query-geojson-proximity">
Query for Locations Near a Point on a Sphere
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/restrictions/#std-label-geospatial-restrictions">
Geospatial Index Restrictions
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2dsphere/query/proximity-to-geojson/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Query for Locations Near a Point on a Sphere
<a class="headerlink leafygreen-ui-1kepx47" href="#query-for-locations-near-a-point-on-a-sphere" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="query-for-locations-near-a-point-on-a-sphere">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#about-this-task" target="_self">
<span>
About this Task
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#before-you-begin" target="_self">
<span>
Before You Begin
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
You can query for location data that appears near a specified point on a
sphere.
</p>
<p class="leafygreen-ui-1kp3ins">
To query for location data near a specified point, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/near/#mongodb-query-op.-near">
<code class="leafygreen-ui-1l06pbn">
$near
</code>
</a>
operator:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.<collection>.find( {\n <location> : {\n $near : {\n $geometry : {\n type : \"Point\",\n coordinates : [ <longitude>, <latitude> ]\n },\n $maxDistance : <distance>\n }\n }\n } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<!-- --><<!-- -->collection<!-- -->><!-- -->.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><location field> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $near : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geometry : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type : "Point",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> coordinates : [ <longitude>, <latitude> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $maxDistance : <distance in meters></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
About this Task
<a class="headerlink leafygreen-ui-1kepx47" href="#about-this-task" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="about-this-task">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When you specify longitude and latitude coordinates, list the
<strong>
longitude
</strong>
first, and then
<strong>
latitude
</strong>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Valid longitude values are between
<code class="leafygreen-ui-1l06pbn">
-180
</code>
and
<code class="leafygreen-ui-1l06pbn">
180
</code>
, both
inclusive.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Valid latitude values are between
<code class="leafygreen-ui-1l06pbn">
-90
</code>
and
<code class="leafygreen-ui-1l06pbn">
90
</code>
, both
inclusive.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Specify distance in the
<code class="leafygreen-ui-1l06pbn">
$maxDistance
</code>
field in
<strong>
meters
</strong>
.
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Before You Begin
<a class="headerlink leafygreen-ui-1kepx47" href="#before-you-begin" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="before-you-begin">
</div>
</a>
</h2>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
places
</code>
collection that contains these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.insertMany( [\n {\n loc: { type: \"Point\", coordinates: [ -73.97, 40.77 ] },\n name: \"Central Park\",\n category : \"Park\"\n },\n {\n loc: { type: \"Point\", coordinates: [ -73.88, 40.78 ] },\n name: \"La Guardia Airport\",\n category: \"Airport\"\n },\n {\n loc: { type: \"Point\", coordinates: [ -1.83, 51.18 ] },\n name: \"Stonehenge\",\n category : \"Monument\"\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">places</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">73.97</span>, <span class="lg-highlight-number">40.77</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Central Park"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->category <!-- -->: <span class="lg-highlight-string">"Park"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">73.88</span>, <span class="lg-highlight-number">40.78</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"La Guardia Airport"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"Airport"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">1.83</span>, <span class="lg-highlight-number">51.18</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Stonehenge"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->category <!-- -->: <span class="lg-highlight-string">"Monument"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The values in the
<code class="leafygreen-ui-1l06pbn">
loc
</code>
field are
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-point">
GeoJSON points
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To query for location data with the
<code class="leafygreen-ui-1l06pbn">
$near
</code>
operator, you must create
a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-geospatial/#std-label-geospatial-index">
geospatial index
</a>
on the field that contains
the location data.
</p>
<p class="leafygreen-ui-1kp3ins">
Create a 2dsphere index on the
<code class="leafygreen-ui-1l06pbn">
loc
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.createIndex( { \"loc\": \"2dsphere\" } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">places</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-string">"loc"</span>: <span class="lg-highlight-string">"2dsphere"</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ol>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use
<code class="leafygreen-ui-1l06pbn">
$near
</code>
to query the collection. The following
<code class="leafygreen-ui-1l06pbn">
$near
</code>
query
returns documents that have a
<code class="leafygreen-ui-1l06pbn">
loc
</code>
field within 5000 meters of a
GeoJSON point located at
<code class="leafygreen-ui-1l06pbn">
[ -73.92, 40.78 ]
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.find( {\n loc: {\n $near: {\n $geometry: {\n type: \"Point\",\n coordinates: [ -73.92, 40.78 ]\n },\n $maxDistance : 5000\n }\n }\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">places</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$near</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geometry</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">73.92</span>, <span class="lg-highlight-number">40.78</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->$maxDistance<!-- --> <!-- -->: <span class="lg-highlight-number">5000</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId(\"63f7c3b15e5eefbdfef81cab\"),\n loc: { type: 'Point', coordinates: [ -73.88, 40.78 ] },\n name: 'La Guardia Airport',\n category: 'Airport'\n },\n {\n _id: ObjectId(\"63f7c3b15e5eefbdfef81caa\"),\n loc: { type: 'Point', coordinates: [ -73.97, 40.77 ] },\n name: 'Central Park',\n category: 'Park'\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"63f7c3b15e5eefbdfef81cab"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'Point'</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">73.88</span>, <span class="lg-highlight-number">40.78</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'La Guardia Airport'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">'Airport'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"63f7c3b15e5eefbdfef81caa"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'Point'</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">73.97</span>, <span class="lg-highlight-number">40.77</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Central Park'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">'Park'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Results are sorted by distance from the queried point, from nearest to
farthest.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/near/#mongodb-query-op.-near">
<code class="leafygreen-ui-1l06pbn">
$near
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/nearSphere/#mongodb-query-op.-nearSphere">
<code class="leafygreen-ui-1l06pbn">
$nearSphere
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/geoNear/#mongodb-pipeline-pipe.-geoNear">
<code class="leafygreen-ui-1l06pbn">
$geoNear
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-point">
<code class="leafygreen-ui-1l06pbn">
Point
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/restrictions/#std-label-geospatial-restrictions">
Geospatial Index Restrictions
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sh.isBalancerRunning/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.isBalancerRunning()
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.isbalancerrunning--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sh.isbalancerrunning--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output-document" target="_self">
<span>
Output Document
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
sh.isBalancerRunning()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.isBalancerRunning" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-sh.isBalancerRunning">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns a document describing the status of the balancer.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for database commands or language-specific drivers,
such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For the database command, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/balancerStatus/#mongodb-dbcommand-dbcmd.balancerStatus">
<code class="leafygreen-ui-1l06pbn">
balancerStatus
</code>
</a>
command
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Output Document
<a class="headerlink leafygreen-ui-1kepx47" href="#output-document" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="output-document">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following is an example of a document returned by the command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n mode: 'full',\n inBalancerRound: false,\n numBalancerRounds: Long(\"1143\"),\n ok: 1,\n '$clusterTime': {\n clusterTime: Timestamp({ t: 1639753724, i: 3 }),\n signature: {\n hash: Binary(Buffer.from(\"0000000000000000000000000000000000000000\", \"hex\"), 0),\n keyId: Long(\"0\")\n }\n },\n operationTime: Timestamp({ t: 1639753724, i: 3 })\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mode<span class="lg-highlight-punctuation">:</span> 'full'<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> inBalancerRound<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-keyword">false</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> numBalancerRounds<span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"1143"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ok<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> '$clusterTime'<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> clusterTime<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-punctuation">{</span> t<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1639753724</span><span class="lg-highlight-punctuation">,</span> i<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">3</span> <span class="lg-highlight-punctuation">}</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> signature<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> hash<span class="lg-highlight-punctuation">:</span> Binary(Buffer.from(<span class="lg-highlight-string">"0000000000000000000000000000000000000000"</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-string">"hex"</span>)<span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> keyId<span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"0"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> operationTime<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-punctuation">{</span> t<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1639753724</span><span class="lg-highlight-punctuation">,</span> i<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">3</span> <span class="lg-highlight-punctuation">}</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
mode
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
String that specifies whether the balancer thread is running
or stopped. Possible values are:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
"full"
</dt>
<dd class="css-1ufxuu7">
Balancer thread is running but not necessarily in a
balancing round.
</dd>
</dl>
</li>
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
"off"
</dt>
<dd class="css-1ufxuu7">
Balancer thread is stopped. Chunk balancing cannot occur in
this mode.
</dd>
</dl>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
inBalancerRound
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Boolean that specifies if the balancer is currently in a
balancing round.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
numBalancerRounds
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of balancer rounds which have occurred since the
config servers were started. This value is reset to 0 when
the config servers are restarted.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
ok
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.runCommand/#std-label-command-response">
Command Response
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
$clusterTime
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.runCommand/#std-label-command-response">
Command Response
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
operationTime
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.runCommand/#std-label-command-response">
Command Response
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.enableBalancing/#mongodb-method-sh.enableBalancing">
<code class="leafygreen-ui-1l06pbn">
sh.enableBalancing()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.disableBalancing/#mongodb-method-sh.disableBalancing">
<code class="leafygreen-ui-1l06pbn">
sh.disableBalancing()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.getBalancerState/#mongodb-method-sh.getBalancerState">
<code class="leafygreen-ui-1l06pbn">
sh.getBalancerState()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.setBalancerState/#mongodb-method-sh.setBalancerState">
<code class="leafygreen-ui-1l06pbn">
sh.setBalancerState()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.startBalancer/#mongodb-method-sh.startBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.startBalancer()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.stopBalancer/#mongodb-method-sh.stopBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.stopBalancer()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.waitForBalancer/#mongodb-method-sh.waitForBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.waitForBalancer()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.waitForBalancerOff/#mongodb-method-sh.waitForBalancerOff">
<code class="leafygreen-ui-1l06pbn">
sh.waitForBalancerOff()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/security-users/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Users in Self-Managed Deployments
<a class="headerlink leafygreen-ui-1kepx47" href="#users-in-self-managed-deployments" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="users-in-self-managed-deployments">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#user-management" target="_self">
<span>
User Management
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#ldap-managed-users" target="_self">
<span>
LDAP Managed Users
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#authentication-database" target="_self">
<span>
Authentication Database
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#centralized-user-data" target="_self">
<span>
Centralized User Data
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#sharded-cluster-users" target="_self">
<span>
Sharded Cluster Users
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#shard-local-users" target="_self">
<span>
Shard Local Users
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
To authenticate a client in MongoDB, you must add a corresponding user
to MongoDB.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
User Management
<a class="headerlink leafygreen-ui-1kepx47" href="#user-management" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="user-management">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can add a user with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.createUser/#mongodb-method-db.createUser">
<code class="leafygreen-ui-1l06pbn">
db.createUser()
</code>
</a>
method using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
. The first user you create must have privileges
to create other users. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-userAdmin">
<code class="leafygreen-ui-1l06pbn">
userAdmin
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-userAdminAnyDatabase">
<code class="leafygreen-ui-1l06pbn">
userAdminAnyDatabase
</code>
</a>
role both confer the privilege to
create other users.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/create-users/">
Create a User on Self-Managed Deployments
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You can grant a user privileges by assigning
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/">
roles
</a>
to the user when you create the user. You can
also grant or revoke roles, as well as update passwords, by updating
existing users. For a full list of user management methods, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/#std-label-user-management-methods">
User Management
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-users-and-roles/">
Manage Users and Roles on Self-Managed Deployments
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
A user is uniquely identified by the user's name and associated
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-authentication-database" target="_self">
<span>
authentication database
</span>
</a>
. MongoDB
associates a user with a unique
<code class="leafygreen-ui-1l06pbn">
userId
</code>
upon creation in MongoDB.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
LDAP Managed Users
<a class="headerlink leafygreen-ui-1kepx47" href="#ldap-managed-users" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="ldap-managed-users">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/#std-label-security-ldap">
LDAP managed users
</a>
created on an LDAP
server do not have an associated document in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/">
system.users
</a>
collection, and therefore don't
have a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/#mongodb-data-admin.system.users.userId">
<code class="leafygreen-ui-1l06pbn">
userId
</code>
</a>
field associated with them.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-authentication-database">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-user-authentication-database">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Authentication Database
<a class="headerlink leafygreen-ui-1kepx47" href="#authentication-database" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="authentication-database">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When you add a user, you create the user in a specific database. The
database you create the user in is the authentication database for the
user.
</p>
<p class="leafygreen-ui-1kp3ins">
However, a user's privileges are not limited to their authentication
database. Therefore, a user can have privileges across different
databases. For more information on roles, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/">
Role-Based Access Control in Self-Managed Deployments
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
A user's name and authentication database serve as a unique identifier
for that user. MongoDB associates a user with a unique
<code class="leafygreen-ui-1l06pbn">
userId
</code>
upon
creation in MongoDB. However,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/#std-label-security-ldap">
LDAP managed users
</a>
created on an LDAP server do not have an associated document in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/#std-label-system-users">
system.users
</a>
collection, and
therefore don't have a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/#mongodb-data-admin.system.users.userId">
<code class="leafygreen-ui-1l06pbn">
userId
</code>
</a>
field
associated with them.
</p>
<p class="leafygreen-ui-1kp3ins">
If two users have the same name but are created in different databases,
they are two separate users. If you want to have a single user with
permissions on multiple databases, create a single user with a role for
each applicable database.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Centralized User Data
<a class="headerlink leafygreen-ui-1kepx47" href="#centralized-user-data" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="centralized-user-data">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For users created in MongoDB, MongoDB stores all user information,
including
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/#mongodb-data-admin.system.users.user">
<code class="leafygreen-ui-1l06pbn">
name
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/#mongodb-data-admin.system.users.credentials">
<code class="leafygreen-ui-1l06pbn">
password
</code>
</a>
, and the user's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/#mongodb-data-admin.system.users.db">
<code class="leafygreen-ui-1l06pbn">
authentication
database
</code>
</a>
, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/">
system.users
</a>
collection in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</p>
<p class="leafygreen-ui-1kp3ins">
Do not modify this collection directly. To manage users, use the
designated
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/#std-label-user-management-commands">
user management commands
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-localhost">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-security">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Sharded Cluster Users
<a class="headerlink leafygreen-ui-1kepx47" href="#sharded-cluster-users" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sharded-cluster-users">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To create users for a sharded cluster, connect to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance and add the users. To authenticate as a
user created on a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance, you must authenticate
through a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance.
</p>
<p class="leafygreen-ui-1kp3ins">
In sharded clusters, MongoDB stores user configuration data in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-server">
config servers
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-shard-local-users">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Shard Local Users
<a class="headerlink leafygreen-ui-1kepx47" href="#shard-local-users" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="shard-local-users">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Some maintenance operations, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/compact/#mongodb-dbcommand-dbcmd.compact">
<code class="leafygreen-ui-1l06pbn">
compact
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#mongodb-method-rs.reconfig">
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
</a>
, require direct connections to specific shards in a
sharded cluster. To perform these operations, you must connect directly to the
shard and authenticate as a
<em>
shard local
</em>
administrative user.
</p>
<p class="leafygreen-ui-1kp3ins">
To create a
<em>
shard local
</em>
administrative user, connect directly to the
primary of the shard and create the user. For instructions on how to
create a shard local user administrator see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/deploy-sharded-cluster-with-keyfile-access-control/">
Deploy Self-Managed Sharded Cluster with Keyfile Authentication
</a>
tutorial.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB stores
<em>
shard local
</em>
users in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database of the
shard itself. These
<em>
shard local
</em>
users are independent from
the users added to the sharded cluster through a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
.
<em>
Shard local
</em>
users are local to the shard and are inaccessible by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, you can use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-directShardOperations">
<code class="leafygreen-ui-1l06pbn">
directShardOperations
</code>
</a>
role to perform maintenance operations
that require you to execute commands directly against a shard.
</p>
<p class="leafygreen-ui-1kp3ins">
Direct connections to a shard should only be used for shard-specific
maintenance and configuration. In general, clients should connect to the sharded
cluster through the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
.
</a>
</p>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Running commands using the
<code class="leafygreen-ui-1l06pbn">
directShardOperations
</code>
role can cause
your cluster to stop working correctly and may cause data corruption.
Only use the
<code class="leafygreen-ui-1l06pbn">
directShardOperations
</code>
role for maintenance purposes
or under the guidance of MongoDB support. Once you are done
performing maintenance operations, stop using the
<code class="leafygreen-ui-1l06pbn">
directShardOperations
</code>
role.
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$geoWithin
<a class="headerlink leafygreen-ui-1kepx47" href="#-geowithin" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-geowithin">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="query">
<dt>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-query-op.-geoWithin" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-query-op.-geoWithin">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Selects documents with geospatial data that exists entirely
within a specified shape.
</p>
<p class="leafygreen-ui-1kp3ins">
The specified shape can be either a GeoJSON
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-polygon">
<code class="leafygreen-ui-1l06pbn">
Polygon
</code>
</a>
(either single-ringed or multi-ringed), a GeoJSON
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-multipolygon">
<code class="leafygreen-ui-1l06pbn">
MultiPolygon
</code>
</a>
, or a shape defined by legacy coordinate
pairs. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
operator uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/geometry/#mongodb-query-op.-geometry">
<code class="leafygreen-ui-1l06pbn">
$geometry
</code>
</a>
operator to specify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geospatial-indexes-store-geojson">
GeoJSON
</a>
object.
</p>
<p class="leafygreen-ui-1kp3ins">
To specify a GeoJSON polygons or multipolygons using the default
coordinate reference system (CRS), use the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n <location>: {\n $geoWithin: {\n $geometry: {\n type: <\"Polygon\" or \"MultiPolygon\"> ,\n coordinates: [ <coordinates> ]\n }\n }\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><location field>: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geoWithin: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geometry: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type: <"Polygon" or "MultiPolygon"> ,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> coordinates: [ <coordinates> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
queries that specify GeoJSON geometries with
areas greater than a single hemisphere, the use of the default CRS
results in queries for the complementary geometries.
</p>
<p class="leafygreen-ui-1kp3ins">
To specify a single-ringed GeoJSON
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-polygon">
polygon
</a>
with a custom MongoDB CRS, use the following
prototype that specifies the custom MongoDB CRS in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/geometry/#mongodb-query-op.-geometry">
<code class="leafygreen-ui-1l06pbn">
$geometry
</code>
</a>
expression:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n <location>: {\n $geoWithin: {\n $geometry: {\n type: \"Polygon\" ,\n coordinates: [ <coordinates> ],\n crs: {\n type: \"name\",\n properties: { name: \"urn:x-mongodb:crs:strictwinding:EPSG:4326\" }\n }\n }\n }\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><location field>: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geoWithin: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geometry: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type: "Polygon" ,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> coordinates: [ <coordinates> ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> crs: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type: "name",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The custom MongoDB CRS uses a counter-clockwise winding order and
allows
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
to support queries with a single-ringed
GeoJSON
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-polygon">
polygon
</a>
whose area is greater than
or equal to a single hemisphere. If the specified polygon is smaller
than a single hemisphere, the behavior of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
with
the MongoDB CRS is the same as with the default CRS. See also
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-geowithin-big-poly" target="_self">
<span>
"Big" Polygons
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If querying for inclusion in a shape defined by legacy coordinate
pairs on a plane, use the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n <location>: {\n $geoWithin: { <shape>: <coordinates> }\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><location field>: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geoWithin: { <shape operator>: <coordinates> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The available shape operators are:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/box/#mongodb-query-op.-box">
<code class="leafygreen-ui-1l06pbn">
$box
</code>
,
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/polygon/#mongodb-query-op.-polygon">
<code class="leafygreen-ui-1l06pbn">
$polygon
</code>
,
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/center/#mongodb-query-op.-center">
<code class="leafygreen-ui-1l06pbn">
$center
</code>
</a>
(defines a circle), and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/centerSphere/#mongodb-query-op.-centerSphere">
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
</a>
(defines a circle on a sphere).
</p>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If you use longitude and latitude, specify coordinates in order
of
<code class="leafygreen-ui-1l06pbn">
longitude, latitude
</code>
.
</p>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Geospatial Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#geospatial-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="geospatial-indexes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
does not require a geospatial index. However, a
geospatial index will improve query performance. Both
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/#std-label-2dsphere-index">
2dsphere
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2d/#std-label-2d-index">
2d
</a>
geospatial indexes support
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
.
</span>
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Unsorted Results
<a class="headerlink leafygreen-ui-1kepx47" href="#unsorted-results" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="unsorted-results">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
operator does not return sorted results. As
such, MongoDB can return
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
queries more quickly than
geospatial
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/near/#mongodb-query-op.-near">
<code class="leafygreen-ui-1l06pbn">
$near
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/nearSphere/#mongodb-query-op.-nearSphere">
<code class="leafygreen-ui-1l06pbn">
$nearSphere
</code>
</a>
queries, which sort
results.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Degenerate Geometry
<a class="headerlink leafygreen-ui-1kepx47" href="#degenerate-geometry" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="degenerate-geometry">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
does not guarantee that it will consider a piece of
geometry to contain its component geometry, or another polygon sharing
its component geometry.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-geowithin-big-poly">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
"Big" Polygons
<a class="headerlink leafygreen-ui-1kepx47" href="#-big--polygons" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-big--polygons">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
, if you specify a single-ringed polygon that
has an area greater than a single hemisphere, include the custom MongoDB
coordinate reference system in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/geometry/#mongodb-query-op.-geometry">
<code class="leafygreen-ui-1l06pbn">
$geometry
</code>
</a>
expression. Otherwise,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
queries for the
complementary geometry. For all other GeoJSON polygons with areas
greater than a hemisphere,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-geoWithin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$geoWithin
</code>
</span>
</a>
queries for the
complementary geometry.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Within a Polygon
<a class="headerlink leafygreen-ui-1kepx47" href="#within-a-polygon" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="within-a-polygon">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example selects all
<code class="leafygreen-ui-1l06pbn">
loc
</code>
data that exist entirely
within a GeoJSON
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-polygon">
<code class="leafygreen-ui-1l06pbn">
Polygon
</code>
</a>
. The area of the polygon is
less than the area of a single hemisphere:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.find(\n {\n loc: {\n $geoWithin: {\n $geometry: {\n type : \"Polygon\" ,\n coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ]\n }\n }\n }\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">places</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geoWithin</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geometry</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->type <!-- -->: <span class="lg-highlight-string">"Polygon"</span> <!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->[ <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">0</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">3</span>, <span class="lg-highlight-number">6</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">6</span>, <span class="lg-highlight-number">1</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">0</span> ] ] ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For single-ringed polygons with areas greater than a single hemisphere,
see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-geowithin-big-polygon" target="_self">
<span>
Within a "Big" Polygon
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-geowithin-big-polygon">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Within a "Big" Polygon
<a class="headerlink leafygreen-ui-1kepx47" href="#within-a--big--polygon" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="within-a--big--polygon">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To query with a single-ringed GeoJSON polygon whose area is greater
than a single hemisphere, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/geometry/#mongodb-query-op.-geometry">
<code class="leafygreen-ui-1l06pbn">
$geometry
</code>
</a>
expression must
specify the custom MongoDB coordinate reference system. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.find(\n {\n loc: {\n $geoWithin: {\n $geometry: {\n type : \"Polygon\" ,\n coordinates: [\n [\n [ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ]\n ]\n ],\n crs: {\n type: \"name\",\n properties: { name: \"urn:x-mongodb:crs:strictwinding:EPSG:4326\" }\n }\n }\n }\n }\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">places</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geoWithin</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geometry</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->type <!-- -->: <span class="lg-highlight-string">"Polygon"</span> <!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">coordinates</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <!-- -->-<span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">60</span> ]<!-- -->, <!-- -->[ <!-- -->-<span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">0</span> ]<!-- -->, <!-- -->[ <!-- -->-<span class="lg-highlight-number">100</span>, <!-- -->-<span class="lg-highlight-number">60</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">100</span>, <!-- -->-<span class="lg-highlight-number">60</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">60</span> ]<!-- -->, <!-- -->[ <!-- -->-<span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">60</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">crs</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"name"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">properties</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"urn:x-mongodb:crs:strictwinding:EPSG:4326"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/perform-maintence-on-replica-set-members/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Perform Maintenance on Self-Managed Replica Set Members
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-maintenance-on-self-managed-replica-set-members" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="perform-maintenance-on-self-managed-replica-set-members">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#overview" target="_self">
<span>
Overview
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Overview
<a class="headerlink leafygreen-ui-1kepx47" href="#overview" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="overview">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
Replica sets
</a>
allow a MongoDB deployment to
remain available during the majority of a maintenance window.
</p>
<p class="leafygreen-ui-1kp3ins">
This document outlines the basic procedure for performing maintenance on
each of the members of a replica set. Furthermore, this particular
sequence strives to minimize the amount of time that the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
is unavailable and control the impact on the
entire deployment.
</p>
<p class="leafygreen-ui-1kp3ins">
Use these steps as the basis for common replica set operations,
particularly for procedures such as upgrading to the latest
version of MongoDB.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For each member of a replica set, starting with a secondary member,
perform the following sequence of events, ending with the primary:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance as a standalone.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Perform the task on the standalone instance.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance as a member of the replica
set.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Stop a secondary.In mongosh, shut down the mongod instance:"},{"@type":"HowToStep","text":"Restart the secondary as a standalone on a different port.At the operating system shell prompt restart mongod\nas a standalone instance.If you are using a configuration file, make the following\nconfiguration updates:Comment out the replication.replSetName option.Change the net.port to a different port.\nMake a note of the original port setting as a comment.Set parameter disableLogicalSessionCacheRefresh to\ntrue in the setParameter section.If the mongod is a shard or\nconfig server member, you must also:Comment out the sharding.clusterRole option.Set parameter skipShardingConfigurationChecks\nto true in the setParameter section.For example, if performing maintenance on a shard/config\nserver replica set member for maintenance, the updated\nconfiguration file will include content like the\nfollowing example:If using command-line options, make the following\nconfiguration updates to restart:Remove --replSetName.Modify --port to a different port.Set parameter disableLogicalSessionCacheRefresh to\ntrue in the --setParameter option.If the mongod is a shard or\nconfig server member, you must also:Remove --shardsvr if a\nshard member and --configsvr if a config server member.Set parameter\nskipShardingConfigurationChecks to\ntrue in the setParameter section.For example, to restart a replica set member that is not part of a sharded cluster:For example, to restart a shard/config server replica set member for maintenance:Before you bind your instance to a publicly-accessible IP address,\nyou must secure your cluster from unauthorized access. For a complete\nlist of security recommendations, see\nSecurity Checklist for Self-Managed Deployments. At minimum, consider\nenabling authentication and hardening\nnetwork infrastructure.Always start mongod with the same user, even when\nrestarting a replica set member as a standalone instance."},{"@type":"HowToStep","text":"Perform maintenance operations on the secondary.While the member is a standalone, use mongosh to\nperform maintenance:While the member is a standalone, no writes are replicated to\nthis member nor are writes on this member replicated to the other\nmembers of the replica set.Ensure that any writes on this standalone do not conflict with\noplog writes that will be applied to the member when it rejoins\nthe replica set."},{"@type":"HowToStep","text":"Restart mongod as a member of the replica set.After performing all maintenance tasks, use the following procedure\nto restart the mongod as a member of the replica set\non its usual port.From mongosh, shut down the standalone\nserver after completing the maintenance:Restart the mongod instance as a replica set\nmember with its original configuration; that is, undo the\nconfiguration changes made when starting as a standalone.Be sure to remove the disableLogicalSessionCacheRefresh\nparameter.For shard or config server members, be sure to remove the\nskipShardingConfigurationChecks parameter.When it has started, connect mongosh to the\nrestarted instance.The secondary takes time to catch up to the primary. From mongosh, use the\nfollowing command to verify that the member has caught up from the\nRECOVERING state to the SECONDARY state."},{"@type":"HowToStep","text":"Perform maintenance on the primary last.To perform maintenance on the primary after completing\nmaintenance tasks on all secondaries, connect\nmongosh to the primary and use\nrs.stepDown() to step down the primary and allow one of\nthe secondaries to be elected the new primary. Specify a 300\nsecond waiting period to prevent the member from being elected\nprimary again for five minutes:After the primary steps down, the replica set will elect a\nnew primary.Restart mongod as a standalone instance, making\nthe following configuration updates.If you are using a configuration file, make the following\nconfiguration updates:Comment out the replication.replSetName option.Change the net.port to a different port.\nMake a note of the original port setting as a comment.Set parameter disableLogicalSessionCacheRefresh to\ntrue in the --setParameter option.If the mongod is a shard or\nconfig server member, you must also:Comment out the sharding.clusterRole option.Set parameter skipShardingConfigurationChecks\nto true in the setParameter section.For example, if performing maintenance on a shard/config\nserver replica set member for maintenance, the updated\nconfiguration file will include content like the\nfollowing example:Before you bind your instance to a publicly-accessible IP address,\nyou must secure your cluster from unauthorized access. For a complete\nlist of security recommendations, see\nSecurity Checklist for Self-Managed Deployments. At minimum, consider\nenabling authentication and hardening\nnetwork infrastructure.If using command-line options, make the following\nconfiguration updates:Remove --replSetName.Modify --port to a different port.Set parameter disableLogicalSessionCacheRefresh to\ntrue in the --setParameter option.If the mongod is a shard or\nconfig server member, you must also:Remove --shardsvr if a\nshard member and --configsvr if a config server member.Set parameter\nskipShardingConfigurationChecks to\ntrue in the setParameter section.For example, to restart a replica set member that is not part of a sharded cluster:For example, to restart a shard/config server replica set member for maintenance:Before you bind your instance to a publicly-accessible IP address,\nyou must secure your cluster from unauthorized access. For a complete\nlist of security recommendations, see\nSecurity Checklist for Self-Managed Deployments. At minimum, consider\nenabling authentication and hardening\nnetwork infrastructure.Perform maintentance task on the now standalone.While the member is a standalone, no writes are replicated to\nthis member nor are writes on this member replicated to the other\nmembers of the replica set.Ensure that any writes on this standalone do not conflict with\noplog writes that will be applied to the member when it rejoins\nthe replica set.After performing all maintenance tasks, restart the\nmongod instance as a replica set member with its\noriginal configuration; that is, undo the configuration changes made\nwhen starting as a standalone.Be sure to remove the disableLogicalSessionCacheRefresh\nparameter.For shard or config server members, be sure to remove the\nskipShardingConfigurationChecks parameter."}],"name":"Procedure","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stop a secondary.
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-a-secondary" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stop-a-secondary">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
, shut down the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.shutdownServer()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">shutdownServer</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Restart the secondary as a standalone on a different port.
<a class="headerlink leafygreen-ui-1kepx47" href="#restart-the-secondary-as-a-standalone-on-a-different-port" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="restart-the-secondary-as-a-standalone-on-a-different-port">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
At the operating system shell prompt restart
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
as a standalone instance.
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-8">
<div aria-label="Tabs to describe usage of command-line/config-file" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_d3ce6dmtt" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_88v9kayv1" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Configuration File
</div>
</button>
<button aria-controls="_jwhifbq0q" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_5efx94su4" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Command-line Options
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_wkx2ygasp" class="" id="_x6plti5sp" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If you are using a configuration file, make the following
configuration updates:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Comment out the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.replSetName">
<code class="leafygreen-ui-1l06pbn">
replication.replSetName
</code>
</a>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Change the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.port">
<code class="leafygreen-ui-1l06pbn">
net.port
</code>
</a>
to a different port.
Make a note of the original port setting as a comment.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set parameter
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</code>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-setParameter">
<code class="leafygreen-ui-1l06pbn">
setParameter
</code>
</a>
section.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shard
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-server">
config server
</a>
member, you must also:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Comment out the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-sharding.clusterRole">
<code class="leafygreen-ui-1l06pbn">
sharding.clusterRole
</code>
</a>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set parameter
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.skipShardingConfigurationChecks">
<code class="leafygreen-ui-1l06pbn">
skipShardingConfigurationChecks
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-setParameter">
<code class="leafygreen-ui-1l06pbn">
setParameter
</code>
</a>
section.
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, if performing maintenance on a shard/config
server replica set member for maintenance, the updated
configuration file will include content like the
following example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"net:\n bindIp: localhost,<hostname(s)|ip>\n port: 27218\n# port: 27018\n#replication:\n# replSetName: shardA\n#sharding:\n# clusterRole: shardsvr\nsetParameter:\n skipShardingConfigurationChecks: true\n disableLogicalSessionCacheRefresh: true","programmingLanguage":"YAML"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light yaml leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bindIp:</span> <span class="lg-highlight-string">localhost,<hostname(s)|ip</span> <span class="lg-highlight-string">address(es)></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">port:</span> <span class="lg-highlight-number">27218</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"># port: 27018</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">#replication:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"># replSetName: shardA</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">#sharding:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"># clusterRole: shardsvr</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">setParameter:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">skipShardingConfigurationChecks:</span> <span class="lg-highlight-literal">true</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">disableLogicalSessionCacheRefresh:</span> <span class="lg-highlight-literal">true</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_9rki0bvei" class="leafygreen-ui-38lglc" id="_67z3rm68r" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If using command-line options, make the following
configuration updates to restart:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Remove
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--replSet">
<code class="leafygreen-ui-1l06pbn">
--replSetName
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Modify
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--port">
<code class="leafygreen-ui-1l06pbn">
--port
</code>
</a>
to a different port.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set parameter
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</code>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--setParameter">
<code class="leafygreen-ui-1l06pbn">
--setParameter
</code>
</a>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shard
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-server">
config server
</a>
member, you must also:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Remove
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--shardsvr">
<code class="leafygreen-ui-1l06pbn">
--shardsvr
</code>
</a>
if a
shard member and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--configsvr">
<code class="leafygreen-ui-1l06pbn">
--configsvr
</code>
</a>
if a config server member.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set parameter
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.skipShardingConfigurationChecks">
<code class="leafygreen-ui-1l06pbn">
skipShardingConfigurationChecks
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-setParameter">
<code class="leafygreen-ui-1l06pbn">
setParameter
</code>
</a>
section.
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, to restart a replica set member that is not part of a sharded cluster:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip> --setParameter disableLogicalSessionCacheRefresh=true","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --port 27218 --dbpath <!-- -->/srv/mongodb<!-- --> --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter disableLogicalSessionCacheRefresh=<span class="lg-highlight-literal">true</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For example, to restart a shard/config server replica set member for maintenance:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip> --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --port 27218 --dbpath <!-- -->/srv/mongodb<!-- --> --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter skipShardingConfigurationChecks=<span class="lg-highlight-literal">true</span> --setParameter disableLogicalSessionCacheRefresh=<span class="lg-highlight-literal">true</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before you bind your instance to a publicly-accessible IP address,
you must secure your cluster from unauthorized access. For a complete
list of security recommendations, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-security-checklist">
Security Checklist for Self-Managed Deployments
</a>
. At minimum, consider
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-checklist-auth">
enabling authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-hardening/#std-label-network-config-hardening">
hardening
network infrastructure
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Always start
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
with the same user, even when
restarting a replica set member as a standalone instance.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Perform maintenance operations on the secondary.
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-maintenance-operations-on-the-secondary" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="perform-maintenance-operations-on-the-secondary">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
While the member is a standalone, use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
to
perform maintenance:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongo --port 27218","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongo --port 27218</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
While the member is a standalone, no writes are replicated to
this member nor are writes on this member replicated to the other
members of the replica set.
</p>
<p class="leafygreen-ui-1kp3ins">
Ensure that any writes on this standalone do not conflict with
oplog writes that will be applied to the member when it rejoins
the replica set.
</p>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
4
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Restart
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
as a member of the replica set.
<a class="headerlink leafygreen-ui-1kepx47" href="#restart-mongod-as-a-member-of-the-replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="restart-mongod-as-a-member-of-the-replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
After performing all maintenance tasks, use the following procedure
to restart the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
as a member of the replica set
on its usual port.
</p>
<p class="leafygreen-ui-1kp3ins">
From
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
, shut down the standalone
server after completing the maintenance:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use admin\ndb.shutdownServer()\n","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use admin</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">shutdownServer</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Restart the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance as a replica set
member with its original configuration; that is, undo the
configuration changes made when starting as a standalone.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Be sure to remove the
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</code>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
For shard or config server members, be sure to remove the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.skipShardingConfigurationChecks">
<code class="leafygreen-ui-1l06pbn">
skipShardingConfigurationChecks
</code>
</a>
parameter.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
When it has started, connect
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
to the
restarted instance.
</p>
<p class="leafygreen-ui-1kp3ins">
The secondary takes time to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-sync/">
catch up to the primary
</a>
. From
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
, use the
following command to verify that the member has caught up from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.RECOVERING">
<code class="leafygreen-ui-1l06pbn">
RECOVERING
</code>
</a>
state to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.SECONDARY">
<code class="leafygreen-ui-1l06pbn">
SECONDARY
</code>
</a>
state.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.status()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">status</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
5
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Perform maintenance on the primary last.
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-maintenance-on-the-primary-last" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="perform-maintenance-on-the-primary-last">
</div>
</a>
</h3>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To perform maintenance on the primary after completing
maintenance tasks on all secondaries, connect
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
to the primary and use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.stepDown/#mongodb-method-rs.stepDown">
<code class="leafygreen-ui-1l06pbn">
rs.stepDown()
</code>
</a>
to step down the primary and allow one of
the secondaries to be elected the new primary. Specify a 300
second waiting period to prevent the member from being elected
primary again for five minutes:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.stepDown(300)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">stepDown</span>(<span class="lg-highlight-number">300</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
After the primary steps down, the replica set will
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-elections/#std-label-replica-set-elections">
elect a
new primary
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
as a standalone instance, making
the following configuration updates.
</p>
</li>
</ol>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-12">
<div aria-label="Tabs to describe usage of command-line/config-file" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_z7ptfvg6h" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_ztie5qvbe" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Configuration File
</div>
</button>
<button aria-controls="_dljfsz1s6" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_jffy2c2lb" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Command-line Options
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_oooei6exl" class="" id="_c5smyr4di" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If you are using a configuration file, make the following
configuration updates:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Comment out the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.replSetName">
<code class="leafygreen-ui-1l06pbn">
replication.replSetName
</code>
</a>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Change the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.port">
<code class="leafygreen-ui-1l06pbn">
net.port
</code>
</a>
to a different port.
Make a note of the original port setting as a comment.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set parameter
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</code>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--setParameter">
<code class="leafygreen-ui-1l06pbn">
--setParameter
</code>
</a>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shard
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-server">
config server
</a>
member, you must also:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Comment out the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-sharding.clusterRole">
<code class="leafygreen-ui-1l06pbn">
sharding.clusterRole
</code>
</a>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set parameter
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.skipShardingConfigurationChecks">
<code class="leafygreen-ui-1l06pbn">
skipShardingConfigurationChecks
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-setParameter">
<code class="leafygreen-ui-1l06pbn">
setParameter
</code>
</a>
section.
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, if performing maintenance on a shard/config
server replica set member for maintenance, the updated
configuration file will include content like the
following example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"net:\n bindIp: localhost,<hostname(s)|ip>\n port: 27218\n# port: 27018\n#replication:\n# replSetName: shardA\n#sharding:\n# clusterRole: shardsvr\nsetParameter:\n skipShardingConfigurationChecks: true\n disableLogicalSessionCacheRefresh: true","programmingLanguage":"YAML"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light yaml leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bindIp:</span> <span class="lg-highlight-string">localhost,<hostname(s)|ip</span> <span class="lg-highlight-string">address(es)></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">port:</span> <span class="lg-highlight-number">27218</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"># port: 27018</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">#replication:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"># replSetName: shardA</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">#sharding:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"># clusterRole: shardsvr</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">setParameter:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">skipShardingConfigurationChecks:</span> <span class="lg-highlight-literal">true</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">disableLogicalSessionCacheRefresh:</span> <span class="lg-highlight-literal">true</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before you bind your instance to a publicly-accessible IP address,
you must secure your cluster from unauthorized access. For a complete
list of security recommendations, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-security-checklist">
Security Checklist for Self-Managed Deployments
</a>
. At minimum, consider
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-checklist-auth">
enabling authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-hardening/#std-label-network-config-hardening">
hardening
network infrastructure
<!-- -->
.
</a>
</p>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_qfzl2ld05" class="leafygreen-ui-38lglc" id="_ijlt4se5q" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If using command-line options, make the following
configuration updates:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Remove
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--replSet">
<code class="leafygreen-ui-1l06pbn">
--replSetName
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Modify
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--port">
<code class="leafygreen-ui-1l06pbn">
--port
</code>
</a>
to a different port.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set parameter
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</code>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--setParameter">
<code class="leafygreen-ui-1l06pbn">
--setParameter
</code>
</a>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shard
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-server">
config server
</a>
member, you must also:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Remove
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--shardsvr">
<code class="leafygreen-ui-1l06pbn">
--shardsvr
</code>
</a>
if a
shard member and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--configsvr">
<code class="leafygreen-ui-1l06pbn">
--configsvr
</code>
</a>
if a config server member.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set parameter
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.skipShardingConfigurationChecks">
<code class="leafygreen-ui-1l06pbn">
skipShardingConfigurationChecks
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-setParameter">
<code class="leafygreen-ui-1l06pbn">
setParameter
</code>
</a>
section.
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, to restart a replica set member that is not part of a sharded cluster:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip> --setParameter disableLogicalSessionCacheRefresh=true","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --port 27218 --dbpath <!-- -->/srv/mongodb<!-- --> --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter disableLogicalSessionCacheRefresh=<span class="lg-highlight-literal">true</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For example, to restart a shard/config server replica set member for maintenance:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --port 27218 --dbpath /srv/mongodb --bind_ip localhost,<hostname(s)|ip> --setParameter skipShardingConfigurationChecks=true --setParameter disableLogicalSessionCacheRefresh=true","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --port 27218 --dbpath <!-- -->/srv/mongodb<!-- --> --bind_ip localhost,<hostname(s)|ip address(es)> --setParameter skipShardingConfigurationChecks=<span class="lg-highlight-literal">true</span> --setParameter disableLogicalSessionCacheRefresh=<span class="lg-highlight-literal">true</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before you bind your instance to a publicly-accessible IP address,
you must secure your cluster from unauthorized access. For a complete
list of security recommendations, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-security-checklist">
Security Checklist for Self-Managed Deployments
</a>
. At minimum, consider
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-checklist-auth">
enabling authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-hardening/#std-label-network-config-hardening">
hardening
network infrastructure
<!-- -->
.
</a>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<ol class="leafygreen-ui-rioki0" start="3" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Perform maintentance task on the now standalone.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
While the member is a standalone, no writes are replicated to
this member nor are writes on this member replicated to the other
members of the replica set.
</p>
<p class="leafygreen-ui-1kp3ins">
Ensure that any writes on this standalone do not conflict with
oplog writes that will be applied to the member when it rejoins
the replica set.
</p>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
After performing all maintenance tasks, restart the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance as a replica set member with its
original configuration; that is, undo the configuration changes made
when starting as a standalone.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Be sure to remove the
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</code>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
For shard or config server members, be sure to remove the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.skipShardingConfigurationChecks">
<code class="leafygreen-ui-1l06pbn">
skipShardingConfigurationChecks
</code>
</a>
parameter.
</p>
</div>
</div>
</li>
</ol>
</section>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/cloneCollectionAsCapped/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
cloneCollectionAsCapped
<a class="headerlink leafygreen-ui-1kepx47" href="#clonecollectionascapped" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="clonecollectionascapped">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
cloneCollectionAsCapped
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.cloneCollectionAsCapped" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.cloneCollectionAsCapped">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.cloneCollectionAsCapped" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cloneCollectionAsCapped
</code>
</span>
</a>
command creates a new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-capped-collection">
capped collection
</a>
from an existing, non-capped collection
within the same database. The operation does not affect the
original non-capped collection.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is not supported in M0, M2, and M5 clusters or in serverless instances.
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n cloneCollectionAsCapped: <existing>,\n toCollection: <capped>,\n size: <capped>,\n writeConcern: <document>,\n comment: <any>\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cloneCollectionAsCapped</span>: <!-- --><existing collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> toCollection: <capped collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> size: <capped size>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> writeConcern: <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> comment: <any></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The command takes the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
cloneCollectionAsCapped
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The name of the existing collection to copy.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
toCollection
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The name of the new capped collection to create.
</p>
<p class="leafygreen-ui-1kp3ins">
The name of the new capped collection must be
distinct and cannot be the same as that of the original existing
collection.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
size
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The maximum size,in bytes, for the capped collection.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
writeConcern
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. A document expressing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/">
write concern
</a>
of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/drop/#mongodb-dbcommand-dbcmd.drop">
<code class="leafygreen-ui-1l06pbn">
drop
</code>
</a>
command.
Omit to use the default write concern.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
comment
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. A user-provided comment to attach to this command. Once set, this
comment appears alongside records of this command in the following
locations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-ref">
mongod log messages
</a>
, in the
<code class="leafygreen-ui-1l06pbn">
attr.command.cursor.comment
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
Database profiler
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#mongodb-data-system.profile.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
<code class="leafygreen-ui-1l06pbn">
currentOp
</code>
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-data-currentOp.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
A comment can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON type
</a>
(string, integer, object, array, etc).
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The command copies an
<code class="leafygreen-ui-1l06pbn">
existing collection
</code>
and creates a new
<code class="leafygreen-ui-1l06pbn">
capped collection
</code>
with a maximum size specified by the
<code class="leafygreen-ui-1l06pbn">
capped
size
</code>
in bytes.
</p>
<p class="leafygreen-ui-1kp3ins">
To replace the original non-capped collection with a
capped collection, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/convertToCapped/#mongodb-dbcommand-dbcmd.convertToCapped">
<code class="leafygreen-ui-1l06pbn">
convertToCapped
</code>
</a>
command.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
capped size
</code>
is less than the size of the source collection,
then not all documents in the source collection will exist in the destination
capped collection.
</p>
<p class="leafygreen-ui-1kp3ins">
This holds a database exclusive lock for the duration of the operation.
Other operations which lock the same database will be blocked until the
operation completes. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/faq/concurrency/#std-label-faq-concurrency-operations-locks">
What locks are taken by some common client operations?
</a>
for
operations that lock the database.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/integral/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$integral (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-integral--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-integral--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<dl class="group">
<dt>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-group-grp.-integral" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-group-grp.-integral">
</div>
</a>
</dt>
<dd>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
Returns the approximation of the area under a curve, which is
calculated using the trapezoidal rule where each set of adjacent
documents form a trapezoid using the:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
field values in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stage for the integration intervals.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-integral-input" target="_self">
<span>
input
</span>
</a>
field
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
result values in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
for the
y axis values.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
is only available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stage.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $integral: {\n input: <expression>,\n unit: <time>\n }\n}</time>"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $integral: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> input: <expression>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> unit: <time unit></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
takes a document with these fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:15%"/>
<col style="width:85%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-integral-input" target="_self">
<span>
input
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-integral-input">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
to
evaluate. You must provide an expression that returns a number.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-integral-unit" target="_self">
<span>
unit
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-integral-unit">
</span>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
string
</code>
that specifies the time unit. Use one of these
strings:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"week"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"day"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"hour"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"minute"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"second"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"millisecond"
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
field is not a date, you
must omit a
<code class="leafygreen-ui-1l06pbn">
unit
</code>
. If you specify a
<code class="leafygreen-ui-1l06pbn">
unit
</code>
, you must specify a date
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
field.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If you omit a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-window">
window
</a>
, a default
window with unbounded upper and lower limits is used.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
powerConsumption
</code>
collection that contains electrical power
usage in kilowatts measured by meter devices at 30 second intervals:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.powerConsumption.insertMany( [\n { powerMeterID: \"1\", timeStamp: new Date( \"2020-05-18T14:10:30Z\" ),\n kilowatts: 2.95 },\n { powerMeterID: \"1\", timeStamp: new Date( \"2020-05-18T14:11:00Z\" ),\n kilowatts: 2.7 },\n { powerMeterID: \"1\", timeStamp: new Date( \"2020-05-18T14:11:30Z\" ),\n kilowatts: 2.6 },\n { powerMeterID: \"1\", timeStamp: new Date( \"2020-05-18T14:12:00Z\" ),\n kilowatts: 2.98 },\n { powerMeterID: \"2\", timeStamp: new Date( \"2020-05-18T14:10:30Z\" ),\n kilowatts: 2.5 },\n { powerMeterID: \"2\", timeStamp: new Date( \"2020-05-18T14:11:00Z\" ),\n kilowatts: 2.25 },\n { powerMeterID: \"2\", timeStamp: new Date( \"2020-05-18T14:11:30Z\" ),\n kilowatts: 2.75 },\n { powerMeterID: \"2\", timeStamp: new Date( \"2020-05-18T14:12:00Z\" ),\n kilowatts: 2.82 }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">powerConsumption</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">powerMeterID</span>: <span class="lg-highlight-string">"1"</span>, <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2020-05-18T14:10:30Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">kilowatts</span>: <span class="lg-highlight-number">2.95</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">powerMeterID</span>: <span class="lg-highlight-string">"1"</span>, <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2020-05-18T14:11:00Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">kilowatts</span>: <span class="lg-highlight-number">2.7</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">powerMeterID</span>: <span class="lg-highlight-string">"1"</span>, <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2020-05-18T14:11:30Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">kilowatts</span>: <span class="lg-highlight-number">2.6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">powerMeterID</span>: <span class="lg-highlight-string">"1"</span>, <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2020-05-18T14:12:00Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">kilowatts</span>: <span class="lg-highlight-number">2.98</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">powerMeterID</span>: <span class="lg-highlight-string">"2"</span>, <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2020-05-18T14:10:30Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">kilowatts</span>: <span class="lg-highlight-number">2.5</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">powerMeterID</span>: <span class="lg-highlight-string">"2"</span>, <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2020-05-18T14:11:00Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">kilowatts</span>: <span class="lg-highlight-number">2.25</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">powerMeterID</span>: <span class="lg-highlight-string">"2"</span>, <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2020-05-18T14:11:30Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">kilowatts</span>: <span class="lg-highlight-number">2.75</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">powerMeterID</span>: <span class="lg-highlight-string">"2"</span>, <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>( <span class="lg-highlight-string">"2020-05-18T14:12:00Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">kilowatts</span>: <span class="lg-highlight-number">2.82</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
This example uses
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stage to output the energy consumption in kilowatt-hours measured
by each meter device:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.powerConsumption.aggregate( [\n {\n $setWindowFields: {\n partitionBy: \"$powerMeterID\",\n sortBy: { timeStamp: 1 },\n output: {\n powerMeterKilowattHours: {\n $integral: {\n input: \"$kilowatts\",\n unit: \"hour\"\n },\n window: {\n range: [ \"unbounded\", \"current\" ],\n unit: \"hour\"\n }\n }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">powerConsumption</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$setWindowFields</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">partitionBy</span>: <span class="lg-highlight-string">"$powerMeterID"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-attr">timeStamp</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">output</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">powerMeterKilowattHours</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$integral</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$kilowatts"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unit</span>: <span class="lg-highlight-string">"hour"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">window</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">range</span>: <!-- -->[ <span class="lg-highlight-string">"unbounded"</span>, <span class="lg-highlight-string">"current"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unit</span>: <span class="lg-highlight-string">"hour"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
partitionBy: "$powerMeterID"
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
partitions
</a>
the documents in the collection by
<code class="leafygreen-ui-1l06pbn">
powerMeterID
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sortBy: { timeStamp: 1 }
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sorts
</a>
the documents in each partition by
<code class="leafygreen-ui-1l06pbn">
timeStamp
</code>
in ascending order (
<code class="leafygreen-ui-1l06pbn">
1
</code>
), so the earliest
<code class="leafygreen-ui-1l06pbn">
timeStamp
</code>
is first.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
output
</code>
sets the
<code class="leafygreen-ui-1l06pbn">
kilowatts
</code>
integral value in a new
field called
<code class="leafygreen-ui-1l06pbn">
powerMeterKilowattHours
</code>
using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
that is run in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-range">
range
</a>
window.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-integral-input" target="_self">
<span>
input
</span>
</a>
expression is set to
<code class="leafygreen-ui-1l06pbn">
"$kilowatts"
</code>
, which is used for the y axis values in the
integral calculation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-integral-unit" target="_self">
<span>
unit
</span>
</a>
is set to
<code class="leafygreen-ui-1l06pbn">
"hour"
</code>
for the
<code class="leafygreen-ui-1l06pbn">
timeStamp
</code>
field, which means
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
returns the kilowatt-hours energy consumption.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-window">
window
</a>
contains documents
between an
<code class="leafygreen-ui-1l06pbn">
unbounded
</code>
lower limit and the
<code class="leafygreen-ui-1l06pbn">
current
</code>
document in
the output. This means
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-integral" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</span>
</a>
returns the total
kilowatt-hours energy consumption for the documents from the
beginning of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
partition
</a>
,
which is the first data point in the partition for each power meter,
to the timestamp of the current document in the output.
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
In this example output, the energy consumption measured by meters 1 and
2 are shown in the
<code class="leafygreen-ui-1l06pbn">
powerMeterKilowattHours
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : ObjectId(\"60cbdc3f833dfeadc8e62863\"), \"powerMeterID\" : \"1\",\n \"timeStamp\" : ISODate(\"2020-05-18T14:10:30Z\"), \"kilowatts\" : 2.95,\n \"powerMeterKilowattHours\" : 0 }\n{ \"_id\" : ObjectId(\"60cbdc3f833dfeadc8e62864\"), \"powerMeterID\" : \"1\",\n \"timeStamp\" : ISODate(\"2020-05-18T14:11:00Z\"), \"kilowatts\" : 2.7,\n \"powerMeterKilowattHours\" : 0.023541666666666666 }\n{ \"_id\" : ObjectId(\"60cbdc3f833dfeadc8e62865\"), \"powerMeterID\" : \"1\",\n \"timeStamp\" : ISODate(\"2020-05-18T14:11:30Z\"), \"kilowatts\" : 2.6,\n \"powerMeterKilowattHours\" : 0.045625 }\n{ \"_id\" : ObjectId(\"60cbdc3f833dfeadc8e62866\"), \"powerMeterID\" : \"1\",\n \"timeStamp\" : ISODate(\"2020-05-18T14:12:00Z\"), \"kilowatts\" : 2.98,\n \"powerMeterKilowattHours\" : 0.068875 }\n{ \"_id\" : ObjectId(\"60cbdc3f833dfeadc8e62867\"), \"powerMeterID\" : \"2\",\n \"timeStamp\" : ISODate(\"2020-05-18T14:10:30Z\"), \"kilowatts\" : 2.5,\n \"powerMeterKilowattHours\" : 0 }\n{ \"_id\" : ObjectId(\"60cbdc3f833dfeadc8e62868\"), \"powerMeterID\" : \"2\",\n \"timeStamp\" : ISODate(\"2020-05-18T14:11:00Z\"), \"kilowatts\" : 2.25,\n \"powerMeterKilowattHours\" : 0.019791666666666666 }\n{ \"_id\" : ObjectId(\"60cbdc3f833dfeadc8e62869\"), \"powerMeterID\" : \"2\",\n \"timeStamp\" : ISODate(\"2020-05-18T14:11:30Z\"), \"kilowatts\" : 2.75,\n \"powerMeterKilowattHours\" : 0.040625 }\n{ \"_id\" : ObjectId(\"60cbdc3f833dfeadc8e6286a\"), \"powerMeterID\" : \"2\",\n \"timeStamp\" : ISODate(\"2020-05-18T14:12:00Z\"), \"kilowatts\" : 2.82,\n \"powerMeterKilowattHours\" : 0.06383333333333334 }","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"60cbdc3f833dfeadc8e62863"</span>)<!-- -->, <span class="lg-highlight-string">"powerMeterID"</span> <!-- -->: <span class="lg-highlight-string">"1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeStamp"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-18T14:10:30Z"</span>)<!-- -->, <span class="lg-highlight-string">"kilowatts"</span> <!-- -->: <span class="lg-highlight-number">2.95</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"powerMeterKilowattHours"</span> <!-- -->: <span class="lg-highlight-number">0</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"60cbdc3f833dfeadc8e62864"</span>)<!-- -->, <span class="lg-highlight-string">"powerMeterID"</span> <!-- -->: <span class="lg-highlight-string">"1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeStamp"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-18T14:11:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"kilowatts"</span> <!-- -->: <span class="lg-highlight-number">2.7</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"powerMeterKilowattHours"</span> <!-- -->: <span class="lg-highlight-number">0.023541666666666666</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"60cbdc3f833dfeadc8e62865"</span>)<!-- -->, <span class="lg-highlight-string">"powerMeterID"</span> <!-- -->: <span class="lg-highlight-string">"1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeStamp"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-18T14:11:30Z"</span>)<!-- -->, <span class="lg-highlight-string">"kilowatts"</span> <!-- -->: <span class="lg-highlight-number">2.6</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"powerMeterKilowattHours"</span> <!-- -->: <span class="lg-highlight-number">0.045625</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"60cbdc3f833dfeadc8e62866"</span>)<!-- -->, <span class="lg-highlight-string">"powerMeterID"</span> <!-- -->: <span class="lg-highlight-string">"1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeStamp"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-18T14:12:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"kilowatts"</span> <!-- -->: <span class="lg-highlight-number">2.98</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"powerMeterKilowattHours"</span> <!-- -->: <span class="lg-highlight-number">0.068875</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"60cbdc3f833dfeadc8e62867"</span>)<!-- -->, <span class="lg-highlight-string">"powerMeterID"</span> <!-- -->: <span class="lg-highlight-string">"2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeStamp"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-18T14:10:30Z"</span>)<!-- -->, <span class="lg-highlight-string">"kilowatts"</span> <!-- -->: <span class="lg-highlight-number">2.5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"powerMeterKilowattHours"</span> <!-- -->: <span class="lg-highlight-number">0</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"60cbdc3f833dfeadc8e62868"</span>)<!-- -->, <span class="lg-highlight-string">"powerMeterID"</span> <!-- -->: <span class="lg-highlight-string">"2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeStamp"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-18T14:11:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"kilowatts"</span> <!-- -->: <span class="lg-highlight-number">2.25</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"powerMeterKilowattHours"</span> <!-- -->: <span class="lg-highlight-number">0.019791666666666666</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"60cbdc3f833dfeadc8e62869"</span>)<!-- -->, <span class="lg-highlight-string">"powerMeterID"</span> <!-- -->: <span class="lg-highlight-string">"2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeStamp"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-18T14:11:30Z"</span>)<!-- -->, <span class="lg-highlight-string">"kilowatts"</span> <!-- -->: <span class="lg-highlight-number">2.75</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"powerMeterKilowattHours"</span> <!-- -->: <span class="lg-highlight-number">0.040625</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"60cbdc3f833dfeadc8e6286a"</span>)<!-- -->, <span class="lg-highlight-string">"powerMeterID"</span> <!-- -->: <span class="lg-highlight-string">"2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeStamp"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-18T14:12:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"kilowatts"</span> <!-- -->: <span class="lg-highlight-number">2.82</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"powerMeterKilowattHours"</span> <!-- -->: <span class="lg-highlight-number">0.06383333333333334</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
For an additional example about
<span class="leafygreen-ui-vm4wms" tabindex="0">
IOT
</span>
Power Consumption, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.practical-mongodb-aggregations.com/examples/time-series/iot-power-consumption.html" rel="noopener noreferrer">
<span>
Practical MongoDB Aggregations
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
e-book.
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/remove-replica-set-member/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Remove Members from a Self-Managed Replica Set
<a class="headerlink leafygreen-ui-1kepx47" href="#remove-members-from-a-self-managed-replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="remove-members-from-a-self-managed-replica-set">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#remove-a-member-using-rs.remove--" target="_self">
<span>
Remove a Member Using
<code class="css-1kekw28 e1wawog0">
rs.remove()
</code>
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#remove-a-member-using-rs.reconfig--" target="_self">
<span>
Remove a Member Using
<code class="css-1kekw28 e1wawog0">
rs.reconfig()
</code>
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
To remove a member of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
</a>
use either of the
following procedures.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Remove a Member Using
<code class="leafygreen-ui-1l06pbn">
rs.remove()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#remove-a-member-using-rs.remove--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="remove-a-member-using-rs.remove--">
</div>
</a>
</h2>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Shut down the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance for the member you wish to
remove. To shut down the instance, connect using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
and use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.shutdownServer/#mongodb-method-db.shutdownServer">
<code class="leafygreen-ui-1l06pbn">
db.shutdownServer()
</code>
</a>
method.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect to the replica set's current
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
. To determine
the current primary, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.hello/#mongodb-method-db.hello">
<code class="leafygreen-ui-1l06pbn">
db.hello()
</code>
</a>
while connected to
any member of the replica set.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.remove/#mongodb-method-rs.remove">
<code class="leafygreen-ui-1l06pbn">
rs.remove()
</code>
</a>
in either of the following forms to
remove the member:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.remove(\"mongod3.example.net:27017\")\nrs.remove(\"mongod3.example.net\")","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">remove</span>(<span class="lg-highlight-string">"mongod3.example.net:27017"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">remove</span>(<span class="lg-highlight-string">"mongod3.example.net"</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
MongoDB may disconnect the shell briefly if the replica set needs to elect a
new primary. The shell then automatically reconnects in such cases. The
shell may display a
<code class="leafygreen-ui-1l06pbn">
DBClientCursor::init call() failed
</code>
error even
though the command succeeds.
</p>
</li>
</ol>
<span class="leafygreen-ui-1qnjd6d" id="std-label-remove-member-using-reconfig">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Remove a Member Using
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#remove-a-member-using-rs.reconfig--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="remove-a-member-using-rs.reconfig--">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can remove a member by reconfiguring the replica set
using a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/">
replica configuration
document
</a>
where that member
is removed from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members">
<code class="leafygreen-ui-1l06pbn">
members
</code>
</a>
array.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#mongodb-method-rs.reconfig">
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
</a>
allows adding or removing no more than
<code class="leafygreen-ui-1l06pbn">
1
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.votes">
<code class="leafygreen-ui-1l06pbn">
voting
</code>
</a>
member at a time. To remove multiple voting
members from the replica set, issue a series of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#mongodb-method-rs.reconfig">
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
</a>
operations to remove one member at a time. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetReconfig/#std-label-replSetReconfig-cmd-single-node">
Reconfiguration Can Add or Remove No More than One Voting Member at a Time
</a>
for more information.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h3>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Shut down the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance for the member you wish to
remove. To shut down the instance, connect using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
and use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.shutdownServer/#mongodb-method-db.shutdownServer">
<code class="leafygreen-ui-1l06pbn">
db.shutdownServer()
</code>
</a>
method.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect to the replica set's current
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
. To determine
the current primary, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.hello/#mongodb-method-db.hello">
<code class="leafygreen-ui-1l06pbn">
db.hello()
</code>
</a>
while connected to
any member of the replica set.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Issue the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.conf/#mongodb-method-rs.conf">
<code class="leafygreen-ui-1l06pbn">
rs.conf()
</code>
</a>
method to view the current
configuration document and determine the position in the
<code class="leafygreen-ui-1l06pbn">
members
</code>
array of the member to remove:
</p>
<div class="leafygreen-ui-vg7qwp" role="note">
<h2 class="leafygreen-ui-2xicti">
Example
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mongod_C.example.net
</code>
is in position
<code class="leafygreen-ui-1l06pbn">
2
</code>
of the
following configuration file:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : \"rs\",\n \"version\" : 7,\n \"members\" : [\n {\n \"_id\" : 0,\n \"host\" : \"mongod_A.example.net:27017\"\n },\n {\n \"_id\" : 1,\n \"host\" : \"mongod_B.example.net:27017\"\n },\n {\n \"_id\" : 2,\n \"host\" : \"mongod_C.example.net:27017\"\n }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"rs"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"version"</span> <!-- -->: <span class="lg-highlight-number">7</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"members"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"mongod_A.example.net:27017"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"mongod_B.example.net:27017"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"mongod_C.example.net:27017"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Assign the current configuration document to the variable
<code class="leafygreen-ui-1l06pbn">
cfg
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cfg = rs.conf()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cfg <!-- -->= <!-- -->rs.<span class="lg-highlight-title lg-highlight-function">conf</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Modify the
<code class="leafygreen-ui-1l06pbn">
cfg
</code>
object to remove the member.
</p>
<div class="leafygreen-ui-vg7qwp" role="note">
<h2 class="leafygreen-ui-2xicti">
Example
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
To remove
<code class="leafygreen-ui-1l06pbn">
mongod_C.example.net:27017
</code>
use the following
JavaScript operation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cfg.members.splice(2,1)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cfg.<span class="lg-highlight-property">members</span>.<span class="lg-highlight-title lg-highlight-function">splice</span>(<span class="lg-highlight-number">2</span>,<span class="lg-highlight-number">1</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Overwrite the replica set configuration document with the new
configuration by issuing the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.reconfig(cfg)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">reconfig</span>(<!-- -->cfg)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To confirm the new configuration, issue
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.conf/#mongodb-method-rs.conf">
<code class="leafygreen-ui-1l06pbn">
rs.conf()
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For the example above the output would be:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : \"rs\",\n \"version\" : 8,\n \"members\" : [\n {\n \"_id\" : 0,\n \"host\" : \"mongod_A.example.net:27017\"\n },\n {\n \"_id\" : 1,\n \"host\" : \"mongod_B.example.net:27017\"\n }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"rs"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"version"</span> <!-- -->: <span class="lg-highlight-number">8</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"members"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"mongod_A.example.net:27017"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"mongod_B.example.net:27017"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ol>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayElemAt/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$arrayElemAt (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-arrayelemat--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-arrayelemat--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#see-also" target="_self">
<span>
See Also
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$arrayElemAt
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-arrayElemAt" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-arrayElemAt">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the element at the specified array index.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can use
<code class="leafygreen-ui-1l06pbn">
$arrayElemAt
</code>
for deployments hosted in the following
environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-arrayElemAt" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$arrayElemAt
</code>
</span>
</a>
has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $arrayElemAt: [ <array>, <idx> ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$arrayElemAt</span>: <!-- -->[ <<!-- -->array<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->idx<!-- -->> <!-- -->] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
<array>
</code>
expression can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to an array.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
<idx>
</code>
expression can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to an integer.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
<idx>
</code>
expression resolves to zero or a positive integer,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-arrayElemAt" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$arrayElemAt
</code>
</span>
</a>
returns the element at the
<code class="leafygreen-ui-1l06pbn">
idx
</code>
position, counting from the start of the array.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
<idx>
</code>
expression resolves to a negative integer,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-arrayElemAt" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$arrayElemAt
</code>
</span>
</a>
returns the element at the
<code class="leafygreen-ui-1l06pbn">
idx
</code>
position, counting from the end of the array.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
idx
</code>
exceeds the array bounds,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-arrayElemAt" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$arrayElemAt
</code>
</span>
</a>
does
not return a result.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
<array>
</code>
expression resolves to an undefined array,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-arrayElemAt" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$arrayElemAt
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
</li>
</ul>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:90%"/>
<col style="width:10%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Results
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $arrayElemAt: [ [ 1, 2, 3 ], 0 ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
1
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $arrayElemAt: [ [ 1, 2, 3 ], -2 ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
2
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $arrayElemAt: [ "$undefinedField", 0 ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
null
</code>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
A collection named
<code class="leafygreen-ui-1l06pbn">
users
</code>
contains the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"name\" : \"dave123\", favorites: [ \"chocolate\", \"cake\", \"butter\", \"apples\" ] }\n{ \"_id\" : 2, \"name\" : \"li\", favorites: [ \"apples\", \"pudding\", \"pie\" ] }\n{ \"_id\" : 3, \"name\" : \"ahn\", favorites: [ \"pears\", \"pecans\", \"chocolate\", \"cherries\" ] }\n{ \"_id\" : 4, \"name\" : \"ty\", favorites: [ \"ice cream\" ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"dave123"</span>, <span class="lg-highlight-attr">favorites</span>: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"cake"</span>, <span class="lg-highlight-string">"butter"</span>, <span class="lg-highlight-string">"apples"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"li"</span>, <span class="lg-highlight-attr">favorites</span>: <!-- -->[ <span class="lg-highlight-string">"apples"</span>, <span class="lg-highlight-string">"pudding"</span>, <span class="lg-highlight-string">"pie"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"ahn"</span>, <span class="lg-highlight-attr">favorites</span>: <!-- -->[ <span class="lg-highlight-string">"pears"</span>, <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"cherries"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"ty"</span>, <span class="lg-highlight-attr">favorites</span>: <!-- -->[ <span class="lg-highlight-string">"ice cream"</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following example returns the first and last element in the
<code class="leafygreen-ui-1l06pbn">
favorites
</code>
array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.users.aggregate([\n {\n $project:\n {\n name: 1,\n first: { $arrayElemAt: [ \"$favorites\", 0 ] },\n last: { $arrayElemAt: [ \"$favorites\", -1 ] }\n }\n }\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">users</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$project</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">first</span>: <!-- -->{ <span class="lg-highlight-attr">$arrayElemAt</span>: <!-- -->[ <span class="lg-highlight-string">"$favorites"</span>, <span class="lg-highlight-number">0</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">last</span>: <!-- -->{ <span class="lg-highlight-attr">$arrayElemAt</span>: <!-- -->[ <span class="lg-highlight-string">"$favorites"</span>, <!-- -->-<span class="lg-highlight-number">1</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"name\" : \"dave123\", \"first\" : \"chocolate\", \"last\" : \"apples\" }\n{ \"_id\" : 2, \"name\" : \"li\", \"first\" : \"apples\", \"last\" : \"pie\" }\n{ \"_id\" : 3, \"name\" : \"ahn\", \"first\" : \"pears\", \"last\" : \"cherries\" }\n{ \"_id\" : 4, \"name\" : \"ty\", \"first\" : \"ice cream\", \"last\" : \"ice cream\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"dave123"</span>, <span class="lg-highlight-string">"first"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"last"</span> <!-- -->: <span class="lg-highlight-string">"apples"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"li"</span>, <span class="lg-highlight-string">"first"</span> <!-- -->: <span class="lg-highlight-string">"apples"</span>, <span class="lg-highlight-string">"last"</span> <!-- -->: <span class="lg-highlight-string">"pie"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"ahn"</span>, <span class="lg-highlight-string">"first"</span> <!-- -->: <span class="lg-highlight-string">"pears"</span>, <span class="lg-highlight-string">"last"</span> <!-- -->: <span class="lg-highlight-string">"cherries"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"ty"</span>, <span class="lg-highlight-string">"first"</span> <!-- -->: <span class="lg-highlight-string">"ice cream"</span>, <span class="lg-highlight-string">"last"</span> <!-- -->: <span class="lg-highlight-string">"ice cream"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
See Also
<a class="headerlink leafygreen-ui-1kepx47" href="#see-also" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="see-also">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/slice/#mongodb-expression-exp.-slice">
<code class="leafygreen-ui-1l06pbn">
$slice
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/first/#mongodb-group-grp.-first">
<code class="leafygreen-ui-1l06pbn">
$first
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/last/#mongodb-group-grp.-last">
<code class="leafygreen-ui-1l06pbn">
$last
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-agg-quick-ref-operator-array">
Array Expression Operators
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/query-element/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Element Query Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#element-query-operators" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="element-query-operators">
</div>
</a>
</h1>
<p class="leafygreen-ui-1kp3ins">
Element operators return data based on field existence or data types.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
For details on a specific operator, including syntax and examples,
click on the link to the operator's reference page.
</p>
</div>
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:70%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/exists/#mongodb-query-op.-exists">
<code class="leafygreen-ui-1l06pbn">
$exists
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Matches documents that have the specified field.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/type/#mongodb-query-op.-type">
<code class="leafygreen-ui-1l06pbn">
$type
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Selects documents if a field is of the specified type.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#operators" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="operators">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#operator-resources" target="_self">
<span>
Operator Resources
</span>
</a>
</li>
</ul>
</div>
<a class="css-53dzkk" href="https://www.mongodb.com/docs/atlas/atlas-ui/agg-pipeline">
<div class="css-z0l49d">
<div class="css-fw7ipj">
<svg aria-label="Cloud Icon" class="leafygreen-ui-1t2jgtk" fill="none" height="16" role="img" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
<path d="M12.571 8.143c0 1.775-.899 3.34-2.267 4.264l-.014.01a5.12 5.12 0 0 1-2.861.869H2.857a2.857 2.857 0 0 1-.545-5.663 5.144 5.144 0 0 1 10.26.52ZM13.821 8.143a6.38 6.38 0 0 1-2.358 4.96 3.429 3.429 0 1 0 2.17-6.506c.123.494.188 1.013.188 1.546Z" fill="currentColor">
</path>
</svg>
</div>
<p class="leafygreen-ui-1kp3ins">
You can
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-ui/agg-pipeline/" target="_self">
<span>
run aggregation pipelines in the UI
</span>
</a>
for deployments hosted in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas.
</span>
</a>
</p>
</div>
</a>
<p class="leafygreen-ui-1kp3ins">
To learn which operators are unsupported in MongoDB Atlas, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands in Atlas.
</span>
</a>
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Operator Resources
<a class="headerlink leafygreen-ui-1kepx47" href="#operator-resources" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="operator-resources">
</div>
</a>
</h2>
<dl>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/">
Query and Projection Operators
</a>
</dt>
<dd class="css-1ufxuu7">
Query operators provide ways to locate data within the database
and projection operators modify how data is presented.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/">
Update Operators
</a>
</dt>
<dd class="css-1ufxuu7">
Update operators are operators that enable you to modify the data
in your database or add additional data.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/">
Aggregation Stages
</a>
</dt>
<dd class="css-1ufxuu7">
Available aggregation stages for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
Aggregation Pipeline.
</a>
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/">
Aggregation Operators
</a>
</dt>
<dd class="css-1ufxuu7">
Aggregation pipeline operations have a collection of operators
available to define and manipulate documents in pipeline stages.
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/ClientEncryption.createEncryptedCollection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
ClientEncryption.createEncryptedCollection()
<a class="headerlink leafygreen-ui-1kepx47" href="#clientencryption.createencryptedcollection--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="clientencryption.createencryptedcollection--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
<span>
Command Fields
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
ClientEncryption.createEncryptedCollection(dbName, collName, clientEncOpts)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-ClientEncryption.createEncryptedCollection" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-ClientEncryption.createEncryptedCollection">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
ClientEncryption.createEncryptedCollection
</code>
creates an
encrypted collection specified by
<code class="leafygreen-ui-1l06pbn">
collName
</code>
on the database
specified by
<code class="leafygreen-ui-1l06pbn">
dbName
</code>
.
</p>
</dd>
</dl>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
ClientEncryption.createEncryptedCollection
</code>
has the
following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"clientEncryption = db.getMongo().getClientEncryption()\n\nclientEncryption.createEncryptedCollection(\n dbName,\n collName,\n {\n provider: kmsProviderName,\n createCollectionOptions: encryptedFieldsMap,\n masterKey: customerMasterKeyCredentials\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">clientEncryption <!-- -->= <!-- -->db.<span class="lg-highlight-title lg-highlight-function">getMongo</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">getClientEncryption</span>(<!-- -->)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">clientEncryption.<span class="lg-highlight-title lg-highlight-function">createEncryptedCollection</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->dbName<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->collName<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">provider</span>: <!-- -->kmsProviderName<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">createCollectionOptions</span>: <!-- -->encryptedFieldsMap<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">masterKey</span>: <!-- -->customerMasterKeyCredentials</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
createEncryptedCollection
</code>
takes these fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Necessity
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
dbName
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Name of the database to encrypt.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
collName
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Name of the collection to encrypt.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
clientEncOpts
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Options to configure the encrypted collection.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
clientEncOpts.provider
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
KMS you are using to store your Customer Master Key.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
clientEncOpts.createCollectionOptions
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Fields to encrypt. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/qe-create-encryption-schema/#std-label-qe-specify-fields-for-encryption">
Steps
</a>
for details on how to configure the
<code class="leafygreen-ui-1l06pbn">
encryptedFieldsMap
</code>
object.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
clientEncOpts.masterKey
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
How to get the master key when the KMS Provider is AWS, GCP, or
Azure.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
client-side field level and queryable
encryption methods require a database connection configured for
client-side encryption. If the current database connection was not
initiated with client-side field level encryption enabled, either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo/#mongodb-method-Mongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
</a>
constructor from the
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
to establish a connection with the required client-side field
level encryption options. The
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
method supports the
following Key Management Service (KMS) providers for Customer
Master Key (CMK) management:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-aws">
Amazon Web Services KMS
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-azure">
Azure Key Vault
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-gcp">
Google Cloud Platform KMS
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-local">
Locally Managed Key
</a>
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<em>
or
</em>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-label-mongosh-client-side-field-level-encryption-options" target="_self">
<span>
command line options
</span>
</a>
to establish a
connection with the required options. The command line options only
support the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-aws">
Amazon Web Services KMS
</a>
provider for CMK management.
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following example uses a locally managed KMS for the
Queryable Encryption configuration.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Start mongoshRun:--nodb means don't connect to a database.Generate a Key StringGenerate a base 64 96-byte string:Create an Encryption Options ObjectTo create a client-side field level encryption options object, use\nthe TEST_LOCAL_KEY string from the previous step:Create an Encrypted Client ObjectTo create an encrypted client object, use the Mongo()\nconstructor. Replace the mongodb://myMongo.example.net URI with\nthe connection string URI for the target\ncluster. For example:","name":"Create Your Encrypted Connection"},{"@type":"HowToStep","text":"Create an encryptedFieldsMaps to specify which fields to encrypt:","name":"Specify which Fields to Encrypt"},{"@type":"HowToStep","text":"Create an encrypted enc.users collection:","name":"Create Your Encrypted Collection"},{"@type":"HowToStep","text":"createEncryptedCollection returns a large result object with many\nfields. Check the value of result.collection to confirm the\ncollection was created in the desired location.","name":"Check Your Result Object"}],"name":"Example","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create Your Encrypted Connection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-your-encrypted-connection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-your-encrypted-connection">
</div>
</a>
</h3>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Start mongosh
</p>
<p class="leafygreen-ui-1kp3ins">
Run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --nodb","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongosh --nodb</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
--nodb
</code>
means don't connect to a database.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Generate a Key String
</p>
<p class="leafygreen-ui-1kp3ins">
Generate a base 64 96-byte string:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const TEST_LOCAL_KEY = require(\"crypto\").randomBytes(96).toString(\"base64\")","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> <span class="lg-highlight-variable lg-highlight-constant">TEST_LOCAL_KEY</span> <!-- -->= <span class="lg-highlight-built_in">require</span>(<span class="lg-highlight-string">"crypto"</span>).<span class="lg-highlight-title lg-highlight-function">randomBytes</span>(<span class="lg-highlight-number">96</span>).<span class="lg-highlight-title lg-highlight-function">toString</span>(<span class="lg-highlight-string">"base64"</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create an Encryption Options Object
</p>
<p class="leafygreen-ui-1kp3ins">
To create a client-side field level encryption options object, use
the
<code class="leafygreen-ui-1l06pbn">
TEST_LOCAL_KEY
</code>
string from the previous step:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" var autoEncryptionOpts = {\n \"keyVaultNamespace\" : \"encryption.__dataKeys\",\n \"kmsProviders\" : {\n \"local\" : {\n \"key\" : BinData(0, TEST_LOCAL_KEY)\n }\n }\n }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> autoEncryptionOpts <!-- -->= <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"keyVaultNamespace"</span> <!-- -->: <span class="lg-highlight-string">"encryption.__dataKeys"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"kmsProviders"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"local"</span> <!-- -->: <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">BinData</span>(<span class="lg-highlight-number">0</span>, <span class="lg-highlight-variable lg-highlight-constant">TEST_LOCAL_KEY</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create an Encrypted Client Object
</p>
<p class="leafygreen-ui-1kp3ins">
To create an encrypted client object, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo/#mongodb-method-Mongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
</a>
constructor. Replace the
<code class="leafygreen-ui-1l06pbn">
mongodb://myMongo.example.net
</code>
URI with
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/connection-string/#std-label-mongodb-uri">
connection string URI
</a>
for the target
cluster. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"encryptedClient = Mongo(\n \"mongodb://myMongo.example.net:27017/?replSetName=myMongo\",\n autoEncryptionOpts\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">encryptedClient <!-- -->= <span class="lg-highlight-title lg-highlight-class">Mongo</span>(</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mongodb://myMongo.example.net:27017/?replSetName=myMongo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->autoEncryptionOpts</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ol>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Specify which Fields to Encrypt
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-which-fields-to-encrypt" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="specify-which-fields-to-encrypt">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create an
<code class="leafygreen-ui-1l06pbn">
encryptedFieldsMaps
</code>
to specify which fields to encrypt:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const encryptedFieldsMap = {\n encryptedFields: {\n fields: [\n {\n path: \"secretField\",\n bsonType: \"string\",\n queries: { queryType: \"equality\" },\n },\n ],\n },\n};","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> encryptedFieldsMap <!-- -->= <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">encryptedFields</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">fields</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"secretField"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"string"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queries</span>: <!-- -->{ <span class="lg-highlight-attr">queryType</span>: <span class="lg-highlight-string">"equality"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create Your Encrypted Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-your-encrypted-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-your-encrypted-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create an encrypted
<code class="leafygreen-ui-1l06pbn">
enc.users
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"clientEncryption = encryptedClient.getClientEncryption();\n\nvar result = clientEncryption.createEncryptedCollection(\n \"enc\",\n \"users\",\n {\n provider: \"local\",\n createCollectionOptions: encryptedFieldsMap,\n masterKey: {} // masterKey is optional when provider is local\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">clientEncryption <!-- -->= <!-- -->encryptedClient.<span class="lg-highlight-title lg-highlight-function">getClientEncryption</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> result <!-- -->= <!-- -->clientEncryption.<span class="lg-highlight-title lg-highlight-function">createEncryptedCollection</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enc"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"users"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">provider</span>: <span class="lg-highlight-string">"local"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">createCollectionOptions</span>: <!-- -->encryptedFieldsMap<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">masterKey</span>: <!-- -->{<!-- -->} <span class="lg-highlight-comment">// masterKey</span><span class="lg-highlight-comment"> is optional when </span><span class="lg-highlight-comment">provider is local</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
4
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Check Your Result Object
<a class="headerlink leafygreen-ui-1kepx47" href="#check-your-result-object" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="check-your-result-object">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
createEncryptedCollection
</code>
returns a large result object with many
fields. Check the value of
<code class="leafygreen-ui-1l06pbn">
result.collection
</code>
to confirm the
collection was created in the desired location.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"enc> result.collection\nenc.users","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">enc<!-- -->> <!-- -->result.<span class="lg-highlight-property">collection</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">enc.<span class="lg-highlight-property">users</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For complete documentation on initiating MongoDB connections with
client-side field level encryption enabled, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo/#mongodb-method-Mongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a complete example of how to create and query an encrypted
collection, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/quick-start/#std-label-qe-quick-start">
Quick Start
<!-- -->
.
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Bulk.find.updateOne/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Bulk.find.updateOne()
<a class="headerlink leafygreen-ui-1kepx47" href="#bulk.find.updateone--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="bulk.find.updateone--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
MongoDB also provides the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.bulkWrite/#mongodb-method-db.collection.bulkWrite">
<code class="leafygreen-ui-1l06pbn">
db.collection.bulkWrite()
</code>
</a>
method for
performing bulk write operations.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
Bulk.find.updateOne(<update>)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Bulk.find.updateOne" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-Bulk.find.updateOne">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Adds a single document update operation to a bulk operations list.
</p>
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find/#mongodb-method-Bulk.find">
<code class="leafygreen-ui-1l06pbn">
Bulk.find()
</code>
</a>
method to specify the condition that
determines which document to update. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Bulk.find.updateOne" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
Bulk.find.updateOne()
</code>
</span>
</a>
method limits the update to a single
document. To update multiple documents, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.update/#mongodb-method-Bulk.find.update">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.update()
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Bulk.find.updateOne" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
Bulk.find.updateOne()
</code>
</span>
</a>
accepts the following parameter:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-bulk-find-updateOne-parameter" target="_self">
<span>
update
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document or pipeline
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-bulk-find-updateOne-parameter">
</span>
<p class="leafygreen-ui-1kp3ins">
The modifications to apply. Can be one of the following:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:40%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A replacement document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Contains only field and value pairs.
</p>
<p class="leafygreen-ui-1kp3ins">
See also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.replaceOne/#mongodb-method-Bulk.find.replaceOne">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.replaceOne()
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Update document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Contains only
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/#std-label-update-operators">
update operator expressions
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Aggregation pipeline
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Contains only the following aggregation stages:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
and its alias
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
and its alias
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
and its alias
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
.
</a>
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on the update modification parameter, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateOne()
</code>
</a>
reference page.
</p>
<p class="leafygreen-ui-1kp3ins">
The sum of the associated
<code class="leafygreen-ui-1l06pbn">
<query>
</code>
document from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find/#mongodb-method-Bulk.find">
<code class="leafygreen-ui-1l06pbn">
Bulk.find()
</code>
</a>
and the update document must be
less than or equal to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-BSON-Document-Size">
maximum BSON document size
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To specify an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-upsert">
upsert: true
</a>
for this operation,
use with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.upsert/#mongodb-method-Bulk.find.upsert">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.upsert()
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To specify
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
to update specific array
elements, use with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.arrayFilters/#mongodb-method-Bulk.find.arrayFilters">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.arrayFilters()
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To specify the index to use for the associated
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find/#mongodb-method-Bulk.find">
<code class="leafygreen-ui-1l06pbn">
Bulk.find()
</code>
</a>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.hint/#mongodb-method-Bulk.find.hint">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.hint()
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To replace a document wholesale, see also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.replaceOne/#mongodb-method-Bulk.find.replaceOne">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.replaceOne()
</code>
.
</a>
</p>
</li>
</ul>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
<update>
</code>
document contains only
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/#std-label-update-operators">
update operator
</a>
expressions, as in:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $set: { status: \"D\" },\n $inc: { points: 2 }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"D"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">2</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Then,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Bulk.find.updateOne" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
Bulk.find.updateOne()
</code>
</span>
</a>
updates only the corresponding
fields,
<code class="leafygreen-ui-1l06pbn">
status
</code>
and
<code class="leafygreen-ui-1l06pbn">
points
</code>
, in the document.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following example initializes a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk/#mongodb-method-Bulk">
<code class="leafygreen-ui-1l06pbn">
Bulk()
</code>
</a>
operations builder
for the
<code class="leafygreen-ui-1l06pbn">
items
</code>
collection, and adds various
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Bulk.find.updateOne" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateOne()
</code>
</span>
</a>
operations to the list of operations.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var bulk = db.items.initializeUnorderedBulkOp();\nbulk.find( { status: \"D\" } ).updateOne( { $set: { status: \"I\", points: \"0\" } } );\nbulk.execute();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> bulk <!-- -->= <!-- -->db.<span class="lg-highlight-property">items</span>.<span class="lg-highlight-title lg-highlight-function">initializeUnorderedBulkOp</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"D"</span> } ).<span class="lg-highlight-title lg-highlight-function">updateOne</span>( <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-string">"0"</span> } } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">execute</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-example-bulk-find-update-one-agg">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update with Aggregation Pipeline
<a class="headerlink leafygreen-ui-1kepx47" href="#update-with-aggregation-pipeline" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="update-with-aggregation-pipeline">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Update methods can accept an aggregation pipeline. For
example, the following uses:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
stage which can provide similar
behavior to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/set/#mongodb-update-up.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
update operator expression,
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the aggregation variable
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/#mongodb-variable-variable.NOW">
<code class="leafygreen-ui-1l06pbn">
NOW
</code>
</a>
, which resolves to the
current datetime and can provide similar behavior to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/currentDate/#mongodb-update-up.-currentDate">
<code class="leafygreen-ui-1l06pbn">
$currentDate
</code>
</a>
update operator expression. To access
aggregation variables, prefix the variable with double dollar signs
<code class="leafygreen-ui-1l06pbn">
$$
</code>
and enclose in quotes.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var bulk = db.items.initializeUnorderedBulkOp();\nbulk.find( { status: \"P\" } ).updateOne(\n [\n { $set: { points: 0, lastModified: \"$$NOW\" } }\n ]\n);\nbulk.execute();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> bulk <!-- -->= <!-- -->db.<span class="lg-highlight-property">items</span>.<span class="lg-highlight-title lg-highlight-function">initializeUnorderedBulkOp</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"P"</span> } ).<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">lastModified</span>: <span class="lg-highlight-string">"$$NOW"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">execute</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.initializeUnorderedBulkOp/#mongodb-method-db.collection.initializeUnorderedBulkOp">
<code class="leafygreen-ui-1l06pbn">
db.collection.initializeUnorderedBulkOp()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.initializeOrderedBulkOp/#mongodb-method-db.collection.initializeOrderedBulkOp">
<code class="leafygreen-ui-1l06pbn">
db.collection.initializeOrderedBulkOp()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find/#mongodb-method-Bulk.find">
<code class="leafygreen-ui-1l06pbn">
Bulk.find()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.update/#mongodb-method-Bulk.find.update">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.update()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.replaceOne/#mongodb-method-Bulk.find.replaceOne">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.replaceOne()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.execute/#mongodb-method-Bulk.execute">
<code class="leafygreen-ui-1l06pbn">
Bulk.execute()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk/#std-label-bulk-methods">
All Bulk Methods
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/csfle/reference/server-side-schema/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
CSFLE Server-Side Schema Enforcement
<a class="headerlink leafygreen-ui-1kepx47" href="#csfle-server-side-schema-enforcement" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="csfle-server-side-schema-enforcement">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
In Client-Side Field Level Encryption (CSFLE)-enabled client applications, you
can use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/manual/core/schema-validation/" target="_self">
<span>
schema validation
</span>
</a>
to have your MongoDB instance enforce encryption of specific fields.
To specify which fields require encryption, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/reference/encryption-schemas/#std-label-field-level-encryption-json-schema">
automatic encryption rule keywords
</a>
with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/jsonSchema/#mongodb-query-op.-jsonSchema">
<code class="leafygreen-ui-1l06pbn">
$jsonSchema
</code>
</a>
validation object. The server rejects any write operations to that collection
where the specified fields are not
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Binary">
<code class="leafygreen-ui-1l06pbn">
Binary (BinData)
</code>
</a>
subtype 6 objects.
</p>
<p class="leafygreen-ui-1kp3ins">
To learn how a CSFLE-enabled client configured to use
automatic encryption behaves when it encounters a server-side schema,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/fundamentals/automatic-encryption/#std-label-field-level-encryption-automatic-remote-schema">
Server-Side Field Level Encryption Enforcement
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To learn how a CSFLE-enabled client configured to use
explicit encryption behaves when it encounters a server-side schema,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/fundamentals/manual-encryption/#std-label-csfle-fundamentals-manual-encryption-server-side-schema">
Server-Side Field Level Encryption Enforcement
<!-- -->
.
</a>
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Consider an
<code class="leafygreen-ui-1l06pbn">
hr
</code>
database with an
<code class="leafygreen-ui-1l06pbn">
employees
</code>
collection.
Documents in the
<code class="leafygreen-ui-1l06pbn">
employees
</code>
collection have the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"name\": \"Jane Doe\",\n \"age\": 51\n}\n","programmingLanguage":"JSON"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"name"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"Jane Doe"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"age"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">51</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You want to enforce the following behavior for client applications
using your collection:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When encrypting the
<code class="leafygreen-ui-1l06pbn">
age
</code>
field, clients must follow these
encryption rules:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the Data Encryption Key with an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
of
<code class="leafygreen-ui-1l06pbn">
UUID("e114f7ad-ad7a-4a68-81a7-ebcb9ea0953a")
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/fundamentals/encryption-algorithms/#std-label-field-level-encryption-random">
randomized
</a>
encryption algorithm.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
age
</code>
field must be an integer.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When encrypting the
<code class="leafygreen-ui-1l06pbn">
name
</code>
field, clients must follow these
encryption rules:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the Data Encryption Key with an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
of
<code class="leafygreen-ui-1l06pbn">
UUID("33408ee9-e499-43f9-89fe-5f8533870617")
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/fundamentals/encryption-algorithms/#std-label-field-level-encryption-deterministic">
deterministic
</a>
encryption algorithm.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
name
</code>
field must be a string.
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
code uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collMod/#mongodb-dbcommand-dbcmd.collMod">
<code class="leafygreen-ui-1l06pbn">
collMod
</code>
</a>
command to update the
<code class="leafygreen-ui-1l06pbn">
hr.employees
</code>
collection to include a
<code class="leafygreen-ui-1l06pbn">
validator
</code>
to enforce the
preceding behavior:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"hr\").runCommand({\n collMod: \"employees\",\n validator: {\n $jsonSchema: {\n bsonType: \"object\",\n properties: {\n age: {\n encrypt: {\n keyId: [UUID(\"e114f7ad-ad7a-4a68-81a7-ebcb9ea0953a\")],\n algorithm: \"AEAD_AES_256_CBC_HMAC_SHA_512-Random\",\n bsonType: \"int\",\n },\n },\n name: {\n encrypt: {\n keyId: [UUID(\"33408ee9-e499-43f9-89fe-5f8533870617\")],\n algorithm: \"AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic\",\n bsonType: \"string\",\n },\n },\n },\n },\n },\n});\n","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"hr"</span>).<span class="lg-highlight-title lg-highlight-function">runCommand</span>(<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">collMod</span>: <span class="lg-highlight-string">"employees"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">validator</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$jsonSchema</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"object"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">properties</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">age</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">encrypt</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyId</span>: <!-- -->[<span class="lg-highlight-title lg-highlight-function">UUID</span>(<span class="lg-highlight-string">"e114f7ad-ad7a-4a68-81a7-ebcb9ea0953a"</span>)]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">algorithm</span>: <span class="lg-highlight-string">"AEAD_AES_256_CBC_HMAC_SHA_512-Random"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"int"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">encrypt</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyId</span>: <!-- -->[<span class="lg-highlight-title lg-highlight-function">UUID</span>(<span class="lg-highlight-string">"33408ee9-e499-43f9-89fe-5f8533870617"</span>)]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">algorithm</span>: <span class="lg-highlight-string">"AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"string"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">})<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To learn more about the encryption algorithms CSFLE supports,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/fundamentals/encryption-algorithms/#std-label-csfle-reference-encryption-algorithms">
Fields and Encryption Types
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To learn more about encryption schemas and encryption rules, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/reference/encryption-schemas/#std-label-csfle-reference-encryption-schemas">
CSFLE Encryption Schemas
<!-- -->
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/views/supported-operations/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Supported Operations for Views
<a class="headerlink leafygreen-ui-1kepx47" href="#supported-operations-for-views" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="supported-operations-for-views">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#database-commands" target="_self">
<span>
Database Commands
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#mongosh-methods" target="_self">
<span>
<code class="css-1kekw28 e1wawog0">
mongosh
</code>
Methods
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
The following operations provide support for views:
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Database Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#database-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="database-commands">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collMod/#mongodb-dbcommand-dbcmd.collMod">
<code class="leafygreen-ui-1l06pbn">
collMod
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/count/#mongodb-dbcommand-dbcmd.count">
<code class="leafygreen-ui-1l06pbn">
count
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/create/#mongodb-dbcommand-dbcmd.create">
<code class="leafygreen-ui-1l06pbn">
create
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/distinct/#mongodb-dbcommand-dbcmd.distinct">
<code class="leafygreen-ui-1l06pbn">
distinct
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/find/#mongodb-dbcommand-dbcmd.find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
Methods
<a class="headerlink leafygreen-ui-1kepx47" href="#mongosh-methods" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongosh-methods">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
db.collection.aggregate()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.count/#mongodb-method-db.collection.count">
<code class="leafygreen-ui-1l06pbn">
db.collection.count()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.countDocuments/#mongodb-method-db.collection.countDocuments">
<code class="leafygreen-ui-1l06pbn">
db.collection.countDocuments()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.distinct/#mongodb-method-db.collection.distinct">
<code class="leafygreen-ui-1l06pbn">
db.collection.distinct()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findOne/#mongodb-method-db.collection.findOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.findOne()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.createCollection/#mongodb-method-db.createCollection">
<code class="leafygreen-ui-1l06pbn">
db.createCollection()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.createView/#mongodb-method-db.createView">
<code class="leafygreen-ui-1l06pbn">
db.createView()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getCollection/#mongodb-method-db.getCollection">
<code class="leafygreen-ui-1l06pbn">
db.getCollection()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getCollectionInfos/#mongodb-method-db.getCollectionInfos">
<code class="leafygreen-ui-1l06pbn">
db.getCollectionInfos()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getCollectionNames/#mongodb-method-db.getCollectionNames">
<code class="leafygreen-ui-1l06pbn">
db.getCollectionNames()
</code>
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/cmp/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$cmp (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-cmp--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-cmp--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$cmp
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-cmp" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-cmp">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Compares two values and returns:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
-1
</code>
if the first value is less than the second.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
1
</code>
if the first value is greater than the second.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
0
</code>
if the two values are equivalent.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cmp" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cmp
</code>
</span>
</a>
compares both value and type, using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order">
specified BSON comparison order
</a>
for values of different types.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$cmp
</code>
has this syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $cmp: [ <expression1>, <expression2> ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$cmp</span>: <!-- -->[ <<!-- -->expression1<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->expression2<!-- -->> <!-- -->] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Create an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.insertMany( [\n { _id : 1, item : \"abc1\", description: \"product 1\", qty: 300 },\n { _id : 2, item : \"abc2\", description: \"product 2\", qty: 200 },\n { _id : 3, item : \"xyz1\", description: \"product 3\", qty: 250 },\n { _id : 4, item : \"VWZ1\", description: \"product 4\", qty: 300 },\n { _id : 5, item : \"VWZ2\", description: \"product 5\", qty: 180 }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->item <!-- -->: <span class="lg-highlight-string">"abc1"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 1"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">300</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->item <!-- -->: <span class="lg-highlight-string">"abc2"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 2"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">200</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->item <!-- -->: <span class="lg-highlight-string">"xyz1"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 3"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">250</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->item <!-- -->: <span class="lg-highlight-string">"VWZ1"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 4"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">300</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">5</span>, <!-- -->item <!-- -->: <span class="lg-highlight-string">"VWZ2"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 5"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">180</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Use the
<code class="leafygreen-ui-1l06pbn">
$cmp
</code>
operator to compare the
<code class="leafygreen-ui-1l06pbn">
qty
</code>
value with
<code class="leafygreen-ui-1l06pbn">
250
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.aggregate(\n [\n {\n $project:\n {\n item: 1,\n qty: 1,\n cmpTo250: { $cmp: [ \"$qty\", 250 ] },\n _id: 0\n }\n }\n ]\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$project</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cmpTo250</span>: <!-- -->{ <span class="lg-highlight-attr">$cmp</span>: <!-- -->[ <span class="lg-highlight-string">"$qty"</span>, <span class="lg-highlight-number">250</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ item : \"abc1\", qty : 300, cmpTo250 : 1 }\n{ item : \"abc2\", qty : 200, cmpTo250 : -1 }\n{ item : \"xyz1\", qty : 250, cmpTo250 : 0 }\n{ item : \"VWZ1\", qty : 300, cmpTo250 : 1 }\n{ item : \"VWZ2\", qty : 180, cmpTo250 : -1 }","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->item <!-- -->: <span class="lg-highlight-string">"abc1"</span>, <!-- -->qty <!-- -->: <span class="lg-highlight-number">300</span>, <!-- -->cmpTo250 <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->item <!-- -->: <span class="lg-highlight-string">"abc2"</span>, <!-- -->qty <!-- -->: <span class="lg-highlight-number">200</span>, <!-- -->cmpTo250 <!-- -->: <!-- -->-<span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->item <!-- -->: <span class="lg-highlight-string">"xyz1"</span>, <!-- -->qty <!-- -->: <span class="lg-highlight-number">250</span>, <!-- -->cmpTo250 <!-- -->: <span class="lg-highlight-number">0</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->item <!-- -->: <span class="lg-highlight-string">"VWZ1"</span>, <!-- -->qty <!-- -->: <span class="lg-highlight-number">300</span>, <!-- -->cmpTo250 <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->item <!-- -->: <span class="lg-highlight-string">"VWZ2"</span>, <!-- -->qty <!-- -->: <span class="lg-highlight-number">180</span>, <!-- -->cmpTo250 <!-- -->: <!-- -->-<span class="lg-highlight-number">1</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/and/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$and (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-and--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-and--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#error-handling" target="_self">
<span>
Error Handling
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$and
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-and" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-and">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Evaluates one or more expressions and returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if
<em>
all
</em>
of
the expressions are
<code class="leafygreen-ui-1l06pbn">
true
</code>
or if run with no argument
expressions. Otherwise,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-and" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$and
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-and" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$and
</code>
</span>
</a>
syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $and: [ <expression1>, <expression2>, ... ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$and</span>: <!-- -->[ <<!-- -->expression1<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->expression2<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
</dd>
</dl>
<span class="leafygreen-ui-1qnjd6d" id="std-label-and-boolean-behavior">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
In addition to the
<code class="leafygreen-ui-1l06pbn">
false
</code>
boolean value,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-and" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$and
</code>
</span>
</a>
evaluates
as
<code class="leafygreen-ui-1l06pbn">
false
</code>
the following:
<code class="leafygreen-ui-1l06pbn">
null
</code>
,
<code class="leafygreen-ui-1l06pbn">
0
</code>
, and
<code class="leafygreen-ui-1l06pbn">
undefined
</code>
values. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-and" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$and
</code>
</span>
</a>
evaluates all other values as
<code class="leafygreen-ui-1l06pbn">
true
</code>
,
including non-zero numeric values and arrays.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:70%"/>
<col style="width:15%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Result
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $and: [ 1, "green" ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
true
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $and: [ ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
true
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $and: [ [ null ], [ false ], [ 0 ] ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
true
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $and: [ null, true ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
false
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $and: [ 0, true ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
false
</code>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Error Handling
<a class="headerlink leafygreen-ui-1kepx47" href="#error-handling" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="error-handling">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To allow the query engine to optimize queries,
<code class="leafygreen-ui-1l06pbn">
$and
</code>
handles
errors as follows:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If any expression supplied to
<code class="leafygreen-ui-1l06pbn">
$and
</code>
would cause an error when
evaluated alone, the
<code class="leafygreen-ui-1l06pbn">
$and
</code>
containing the expression may cause an
error but an error is not guaranteed.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An expression supplied after the first expression supplied to
<code class="leafygreen-ui-1l06pbn">
$and
</code>
may cause an error even if the first expression evaluates to
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, the following query
<em>
always
</em>
produces an error if
<code class="leafygreen-ui-1l06pbn">
$x
</code>
is
<code class="leafygreen-ui-1l06pbn">
0
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.example.find( {\n $expr: { $eq: [ { $divide: [ 1, \"$x\" ] }, 3 ] }\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">example</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$divide</span>: <!-- -->[ <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"$x"</span> ] }<!-- -->, <span class="lg-highlight-number">3</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following query, which contains multiple expressions supplied to
<code class="leafygreen-ui-1l06pbn">
$and
</code>
,
<em>
may
</em>
produce an error if there is any document where
<code class="leafygreen-ui-1l06pbn">
$x
</code>
is
<code class="leafygreen-ui-1l06pbn">
0
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.example.find( {\n $and: [\n { x: { $ne: 0 } },\n { $expr: { $eq: [ { $divide: [ 1, \"$x\" ] }, 3 ] } }\n ]\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">example</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$and</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">x</span>: <!-- -->{ <span class="lg-highlight-attr">$ne</span>: <span class="lg-highlight-number">0</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$divide</span>: <!-- -->[ <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"$x"</span> ] }<!-- -->, <span class="lg-highlight-number">3</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Create an example
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.insertMany([\n { \"_id\" : 1, \"item\" : \"abc1\", description: \"product 1\", qty: 300 },\n { \"_id\" : 2, \"item\" : \"abc2\", description: \"product 2\", qty: 200 },\n { \"_id\" : 3, \"item\" : \"xyz1\", description: \"product 3\", qty: 250 },\n { \"_id\" : 4, \"item\" : \"VWZ1\", description: \"product 4\", qty: 300 },\n { \"_id\" : 5, \"item\" : \"VWZ2\", description: \"product 5\", qty: 180 }\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"abc1"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 1"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">300</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"abc2"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 2"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">200</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"xyz1"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 3"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">250</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"VWZ1"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 4"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">300</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"VWZ2"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"product 5"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">180</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
This operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-and" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$and
</code>
</span>
</a>
operator to
determine if
<code class="leafygreen-ui-1l06pbn">
qty
</code>
is greater than 100
<em>
and
</em>
less than
<code class="leafygreen-ui-1l06pbn">
250
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.aggregate(\n [\n {\n $project:\n {\n item: 1,\n qty: 1,\n result: { $and: [ { $gt: [ \"$qty\", 100 ] }, { $lt: [ \"$qty\", 250 ] } ] }\n }\n }\n ]\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$project</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">result</span>: <!-- -->{ <span class="lg-highlight-attr">$and</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <!-- -->[ <span class="lg-highlight-string">"$qty"</span>, <span class="lg-highlight-number">100</span> ] }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">$lt</span>: <!-- -->[ <span class="lg-highlight-string">"$qty"</span>, <span class="lg-highlight-number">250</span> ] } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns these results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"item\" : \"abc1\", \"qty\" : 300, \"result\" : false }\n{ \"_id\" : 2, \"item\" : \"abc2\", \"qty\" : 200, \"result\" : true }\n{ \"_id\" : 3, \"item\" : \"xyz1\", \"qty\" : 250, \"result\" : false }\n{ \"_id\" : 4, \"item\" : \"VWZ1\", \"qty\" : 300, \"result\" : false }\n{ \"_id\" : 5, \"item\" : \"VWZ2\", \"qty\" : 180, \"result\" : true }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"abc1"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">300</span>, <span class="lg-highlight-string">"result"</span> <!-- -->: <span class="lg-highlight-literal">false</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"abc2"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">200</span>, <span class="lg-highlight-string">"result"</span> <!-- -->: <span class="lg-highlight-literal">true</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"xyz1"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">250</span>, <span class="lg-highlight-string">"result"</span> <!-- -->: <span class="lg-highlight-literal">false</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"VWZ1"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">300</span>, <span class="lg-highlight-string">"result"</span> <!-- -->: <span class="lg-highlight-literal">false</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"VWZ2"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">180</span>, <span class="lg-highlight-string">"result"</span> <!-- -->: <span class="lg-highlight-literal">true</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sh.waitForBalancer/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.waitForBalancer()
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.waitforbalancer--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sh.waitforbalancer--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
sh.waitForBalancer(wait, timeout, interval)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.waitForBalancer" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-sh.waitForBalancer">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Waits for a change in the state of the balancer.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.waitForBalancer" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.waitForBalancer()
</code>
</span>
</a>
is an internal method, which takes
the following arguments:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
wait
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. Set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
to ensure the balancer is now active. The
default is
<code class="leafygreen-ui-1l06pbn">
false
</code>
, which waits until balancing stops
and becomes inactive.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
timeout
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Milliseconds to wait.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
interval
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Milliseconds to sleep.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/logApplicationMessage/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
logApplicationMessage
<a class="headerlink leafygreen-ui-1kepx47" href="#logapplicationmessage" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="logapplicationmessage">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
logApplicationMessage
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.logApplicationMessage" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.logApplicationMessage">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.logApplicationMessage" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
logApplicationMessage
</code>
</span>
</a>
command allows users to post
a custom message to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/auditing/#std-label-auditing">
audit
</a>
log. If
running with authorization, users must have
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-clusterAdmin">
<code class="leafygreen-ui-1l06pbn">
clusterAdmin
</code>
</a>
role, or roles that inherit from
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-clusterAdmin">
<code class="leafygreen-ui-1l06pbn">
clusterAdmin
</code>
</a>
, to run
the command.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is supported in all MongoDB Atlas clusters.
For information on Atlas support for all commands, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n logApplicationMessage: <string>\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logApplicationMessage</span>: <<!-- -->string<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB associates these custom messages with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/audit-message/mongo/#std-label-audit-action-details-results">
audit
operation
</a>
<code class="leafygreen-ui-1l06pbn">
applicationMessage
</code>
,
and the messages are subject to any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-audit-filters/#std-label-audit-filter">
filtering
<!-- -->
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Mongo.setCausalConsistency/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Mongo.setCausalConsistency()
<a class="headerlink leafygreen-ui-1kepx47" href="#mongo.setcausalconsistency--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongo.setcausalconsistency--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
Mongo.setCausalConsistency(<boolean>)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Mongo.setCausalConsistency" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-Mongo.setCausalConsistency">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Enables or disables
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-isolation-consistency-recency/#std-label-causal-consistency">
causal consistency
</a>
on the connection object. Causal consistency is disabled on the
connection object by default.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo/#mongodb-method-Mongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
</a>
connection object may have causal consistency
disabled even though sessions may have causal consistency enabled
or vice versa. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo.startSession/#mongodb-method-Mongo.startSession">
<code class="leafygreen-ui-1l06pbn">
Mongo.startSession()
</code>
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To enable causal consistency for the connection object, call the
method without any argument:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var conn = Mongo(\"localhost:27017\");\nconn.setCausalConsistency();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> conn <!-- -->= <span class="lg-highlight-title lg-highlight-class">Mongo</span>(<span class="lg-highlight-string">"localhost:27017"</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">conn.<span class="lg-highlight-title lg-highlight-function">setCausalConsistency</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The method also can accept a boolean argument:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
true
</code>
to enable causal consistency:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"conn.setCausalConsistency(true);","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">conn.<span class="lg-highlight-title lg-highlight-function">setCausalConsistency</span>(<span class="lg-highlight-literal">true</span>)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
false
</code>
to disable causal consistency:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"conn.setCausalConsistency(false);","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">conn.<span class="lg-highlight-title lg-highlight-function">setCausalConsistency</span>(<span class="lg-highlight-literal">false</span>)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ul>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
operation enables causal
consistency on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo/#mongodb-method-Mongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
</a>
connection object associated with
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
's global
<code class="leafygreen-ui-1l06pbn">
db
</code>
variable:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getMongo().setCausalConsistency();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getMongo</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">setCausalConsistency</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getMongo/#mongodb-method-db.getMongo">
<code class="leafygreen-ui-1l06pbn">
db.getMongo()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/revokePrivilegesFromRole/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
revokePrivilegesFromRole
<a class="headerlink leafygreen-ui-1kepx47" href="#revokeprivilegesfromrole" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="revokeprivilegesfromrole">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
<span>
Command Fields
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#required-access" target="_self">
<span>
Required Access
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
revokePrivilegesFromRole
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.revokePrivilegesFromRole" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.revokePrivilegesFromRole">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Removes the specified privileges from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-user-defined-roles/#std-label-user-defined-roles">
user-defined
</a>
role on the database where the
command is run.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
In
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#std-program-mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
, this command can also be run through the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.revokePrivilegesFromRole/#mongodb-method-db.revokePrivilegesFromRole">
<code class="leafygreen-ui-1l06pbn">
db.revokePrivilegesFromRole()
</code>
</a>
helper method
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
Helper methods are convenient for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#std-program-mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
users, but they may not
return the same level of information as database commands. In cases where
the convenience is not needed or the additional return fields are required,
use the database command.
</p>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is not supported in M0, M2, and M5 clusters.
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n revokePrivilegesFromRole: \"<role>\",\n privileges: [\n { resource: { <resource> }, actions: [ \"<action>\", ... ] },\n ...\n ],\n writeConcern: <write>,\n comment: <any>\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">revokePrivilegesFromRole</span>: <span class="lg-highlight-string">"<role>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <<!-- -->resource<!-- -->> <!-- -->}<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"<action>"</span>, <!-- -->...<!-- --> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">writeConcern</span>: <!-- --><write concern document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> comment: <any></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command takes the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
revokePrivilegesFromRole
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-user-defined-roles/#std-label-user-defined-roles">
user-defined
</a>
role to revoke
privileges from.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
privileges
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
An array of privileges to remove from the role. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-roles-collection/#mongodb-data-admin.system.roles.privileges">
<code class="leafygreen-ui-1l06pbn">
privileges
</code>
</a>
for more information on the
format of the privileges.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
writeConcern
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. The level of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
write concern
</a>
for the
operation. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-wc-specs">
Write Concern Specification
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
comment
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
any
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. A user-provided comment to attach to this command. Once set, this
comment appears alongside records of this command in the following
locations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-ref">
mongod log messages
</a>
, in the
<code class="leafygreen-ui-1l06pbn">
attr.command.cursor.comment
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
Database profiler
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#mongodb-data-system.profile.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
<code class="leafygreen-ui-1l06pbn">
currentOp
</code>
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-data-currentOp.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
A comment can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON type
</a>
(string, integer, object, array, etc).
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To revoke a privilege, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/">
resource document
</a>
pattern must match
<strong>
exactly
</strong>
the
<code class="leafygreen-ui-1l06pbn">
resource
</code>
field of that privilege. The
<code class="leafygreen-ui-1l06pbn">
actions
</code>
field can be a
subset or match exactly.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider the role
<code class="leafygreen-ui-1l06pbn">
accountRole
</code>
in the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database with the following privilege that specifies the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database as the resource:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"resource\" : {\n \"db\" : \"products\",\n \"collection\" : \"\"\n },\n \"actions\" : [\n \"find\",\n \"update\"\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resource"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"products"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"collection"</span> <!-- -->: <span class="lg-highlight-string">""</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"actions"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"find"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"update"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You
<em>
cannot
</em>
revoke
<code class="leafygreen-ui-1l06pbn">
find
</code>
and/or
<code class="leafygreen-ui-1l06pbn">
update
</code>
from just
<em>
one
</em>
collection in the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database. The following operations
result in no change to the role:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use products\ndb.runCommand(\n {\n revokePrivilegesFromRole: \"accountRole\",\n privileges:\n [\n {\n resource : {\n db : \"products\",\n collection : \"gadgets\"\n },\n actions : [\n \"find\",\n \"update\"\n ]\n }\n ]\n }\n)\n\ndb.runCommand(\n {\n revokePrivilegesFromRole: \"accountRole\",\n privileges:\n [\n {\n resource : {\n db : \"products\",\n collection : \"gadgets\"\n },\n actions : [\n \"find\"\n ]\n }\n ]\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use products</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">revokePrivilegesFromRole</span>: <span class="lg-highlight-string">"accountRole"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->resource <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->db <!-- -->: <span class="lg-highlight-string">"products"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->collection <!-- -->: <span class="lg-highlight-string">"gadgets"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->actions <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"find"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"update"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">revokePrivilegesFromRole</span>: <span class="lg-highlight-string">"accountRole"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->resource <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->db <!-- -->: <span class="lg-highlight-string">"products"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->collection <!-- -->: <span class="lg-highlight-string">"gadgets"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->actions <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"find"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To revoke the
<code class="leafygreen-ui-1l06pbn">
"find"
</code>
and/or the
<code class="leafygreen-ui-1l06pbn">
"update"
</code>
action from the role
<code class="leafygreen-ui-1l06pbn">
accountRole
</code>
, you must match the resource document exactly. For
example, the following operation revokes just the
<code class="leafygreen-ui-1l06pbn">
"find"
</code>
action
from the existing privilege.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use products\ndb.runCommand(\n {\n revokePrivilegesFromRole: \"accountRole\",\n privileges:\n [\n {\n resource : {\n db : \"products\",\n collection : \"\"\n },\n actions : [\n \"find\"\n ]\n }\n ]\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use products</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">revokePrivilegesFromRole</span>: <span class="lg-highlight-string">"accountRole"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->resource <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->db <!-- -->: <span class="lg-highlight-string">"products"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->collection <!-- -->: <span class="lg-highlight-string">""</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->actions <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"find"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Required Access
<a class="headerlink leafygreen-ui-1kepx47" href="#required-access" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="required-access">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You must have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-revokeRole">
<code class="leafygreen-ui-1l06pbn">
revokeRole
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
action
</a>
on the database a privilege targets in order to
revoke that privilege. If the privilege targets multiple databases or the
<code class="leafygreen-ui-1l06pbn">
cluster
</code>
resource, you must have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-revokeRole">
<code class="leafygreen-ui-1l06pbn">
revokeRole
</code>
</a>
action
on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following operation removes multiple privileges from the
<code class="leafygreen-ui-1l06pbn">
associates
</code>
role in the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use products\ndb.runCommand(\n {\n revokePrivilegesFromRole: \"associate\",\n privileges:\n [\n {\n resource: { db: \"products\", collection: \"\" },\n actions: [ \"createCollection\", \"createIndex\", \"find\" ]\n },\n {\n resource: { db: \"products\", collection: \"orders\" },\n actions: [ \"insert\" ]\n }\n ],\n writeConcern: { w: \"majority\" }\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use products</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">revokePrivilegesFromRole</span>: <span class="lg-highlight-string">"associate"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"products"</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">""</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"createCollection"</span>, <span class="lg-highlight-string">"createIndex"</span>, <span class="lg-highlight-string">"find"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"products"</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">"orders"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"insert"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">writeConcern</span>: <!-- -->{ <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-string">"majority"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sp.processor.stop/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sp.processor.stop()
<a class="headerlink leafygreen-ui-1kepx47" href="#sp.processor.stop--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sp.processor.stop--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
<span>
Command Fields
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#access-control" target="_self">
<span>
Access Control
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
sp.processor.stop()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sp.processor.stop" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-sp.processor.stop">
</div>
</a>
</dt>
<dd>
</dd>
</dl>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
:
<!-- -->
Stops a named
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-sp/overview/#mongodb-expression-exp.Stream-Processor" target="_self">
<span>
Stream Processor
</span>
</a>
on
the current
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-sp/overview/#mongodb-expression-exp.Stream-Processing-Instance" target="_self">
<span>
Stream Processing Instance
<!-- -->
.
</span>
</a>
</p>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sp.processor.stop" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sp.processor.stop()
</code>
</span>
</a>
method has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sp.processor.stop()","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sp.processor.stop()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sp.processor.stop()
</code>
takes no fields.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sp.processor.stop()
</code>
stops a named stream processor on the
current stream processing instance. The stream processor must be in a
<code class="leafygreen-ui-1l06pbn">
running
</code>
state. If you invoke
<code class="leafygreen-ui-1l06pbn">
sp.processor.stop()
</code>
for a
stream processor that is not
<code class="leafygreen-ui-1l06pbn">
running
</code>
,
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
will return an error.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Access Control
<a class="headerlink leafygreen-ui-1kepx47" href="#access-control" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="access-control">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The user running
<code class="leafygreen-ui-1l06pbn">
sp.processor.stop()
</code>
must have the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/mongodb-users-roles-and-privileges/#mongodb-atlasrole-atlasAdmin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
atlasAdmin
</code>
</span>
</a>
role.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following example stops a stream processor named
<code class="leafygreen-ui-1l06pbn">
solarDemo
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sp.solarDemo.stop()"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sp.solarDemo.stop()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-sp/manage-stream-processor/" target="_self">
<span>
Manage Stream Processors
</span>
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sh.disableBalancing/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.disableBalancing()
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.disablebalancing--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sh.disablebalancing--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
sh.disableBalancing(namespace)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.disableBalancing" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-sh.disableBalancing">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Disables the balancer for the
specified sharded collection. This does not affect the balancing of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunks
</a>
for other sharded collections in the same cluster.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
You can only run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.disableBalancing" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.disableBalancing()
</code>
</span>
</a>
on a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.disableBalancing" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.disableBalancing()
</code>
</span>
</a>
errors if run on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.disableBalancing" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.disableBalancing()
</code>
</span>
</a>
method has the following
parameter:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
namespace
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-namespace">
namespace
</a>
of the collection.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on the balancing process, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/">
Manage Sharded Cluster Balancer
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing">
Sharded Cluster Balancer
<!-- -->
.
</a>
</p>
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sh.abortMoveCollection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.abortMoveCollection()
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.abortmovecollection--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sh.abortmovecollection--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#parameters" target="_self">
<span>
Parameters
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
sh.abortMoveCollection(namespace)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.abortMoveCollection" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-sh.abortMoveCollection">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Stops an in-progress
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveCollection/#mongodb-dbcommand-dbcmd.moveCollection">
<code class="leafygreen-ui-1l06pbn">
moveCollection
</code>
</a>
operation.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
This method must run on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is not available on the Atlas Shared Tier or on Atlas
Serverless.
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.abortMoveCollection( \"<namespace>.<collection>\" )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">abortMoveCollection</span>( <span class="lg-highlight-string">"<namespace>.<collection>"</span> )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Parameters
<a class="headerlink leafygreen-ui-1kepx47" href="#parameters" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="parameters">
</div>
</a>
</h2>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:10%"/>
<col style="width:10%"/>
<col style="width:60%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Necessity
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
coll
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the database and collection to stop moving.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following example stops the in-progress move of the
<code class="leafygreen-ui-1l06pbn">
sales.us_accounts
</code>
collection to a new shard.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.abortMoveCollection( \"sales.us_accounts\" )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">abortMoveCollection</span>( <span class="lg-highlight-string">"sales.us_accounts"</span> )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/abortMoveCollection/#mongodb-dbcommand-dbcmd.abortMoveCollection">
<code class="leafygreen-ui-1l06pbn">
abortMoveCollection
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/stop-moving-a-collection/#std-label-task-stop-moving-a-collection">
Stop Moving a Collection
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/inconsistency-type/RoutingTableMissingMinKey/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
RoutingTableMissingMinKey
<a class="headerlink leafygreen-ui-1kepx47" href="#routingtablemissingminkey" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="routingtablemissingminkey">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#format" target="_self">
<span>
Format
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
RoutingTableMissingMinKey
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-RoutingTableMissingMinKey" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-RoutingTableMissingMinKey">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
A range gap exists in the routing table. The first chunk does not start from
the minimum key value.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Format
<a class="headerlink leafygreen-ui-1kepx47" href="#format" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="format">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n type: \"RoutingTableMissingMinKey\",\n description: \"<string>\",\n details: {\n namespace: \"<string>\",\n collectionUUID: <string>,\n minKeyObj: { <document> },\n globalMin: { <document> }\n }\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"RoutingTableMissingMinKey"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> description<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"<string>"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> details<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> namespace<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"<string>"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> collectionUUID<span class="lg-highlight-punctuation">:</span> <string><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> minKeyObj<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span> <document> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> globalMin<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span> <document> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
RoutingTableMissingMinKey
</code>
inconsistency documents contain the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:60%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
type
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Type of inconsistency document. Returns a value of
<code class="leafygreen-ui-1l06pbn">
RoutingTableMissingMinKey
</code>
for this
inconsistency type.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
description
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Describes the inconsistency found in the metadata.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Document with additional information on where the inconsistency exists in the
database and sharded cluster.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.namespace
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Indicates the database and collection where the inconsistency exists.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.collectionUUID
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
UUID
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
UUID of the collection.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.minKeyObj
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The minimum key used by the collection.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.globalMin
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The expected minimum key of the collection.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.adminCommand/#mongodb-method-db.adminCommand">
<code class="leafygreen-ui-1l06pbn">
db.adminCommand()
</code>
</a>
method to call the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/checkMetadataConsistency/#mongodb-dbcommand-dbcmd.checkMetadataConsistency">
<code class="leafygreen-ui-1l06pbn">
checkMetadataConsistency
</code>
</a>
command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { checkMetadataConsistency: 1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{ <span class="lg-highlight-attr">checkMetadataConsistency</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The method returns a cursor with a batch of documents showing the inconsistencies
found in the sharding metadata. The example below shows a cursor with
a
<code class="leafygreen-ui-1l06pbn">
RoutingTableMissingMinKey
</code>
inconsistency document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n cursor: {\n id: Long(\"0\"),\n ns: \"test.$cmd.aggregate\",\n firstBatch: [\n {\n type: \"RoutingTableMissingMinKey\",\n description: \"Routing table has a gap because first chunk does not start from MinKey\",\n details: {\n namespace: \"test.authors\",\n collectionUUID: new UUID(\"62ebdb7e-a7bb-4151-a620-49d44cef097f\"),\n minKeyObj: {\n x: 0\n },\n globalMin: {\n x: MinKey\n }\n }\n }\n ],\n },\n ok: 1\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> id<span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"0"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ns<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.$cmd.aggregate"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> firstBatch<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> type<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"RoutingTableMissingMinKey"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> description<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"Routing table has a gap because first chunk does not start from MinKey"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> details<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> namespace<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.authors"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> collectionUUID<span class="lg-highlight-punctuation">:</span> new UUID(<span class="lg-highlight-string">"62ebdb7e-a7bb-4151-a620-49d44cef097f"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> minKeyObj<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> x<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">0</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> globalMin<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> x<span class="lg-highlight-punctuation">:</span> MinKey</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">]</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ok<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/restart-sharded-cluster/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Restart a Self-Managed Sharded Cluster
<a class="headerlink leafygreen-ui-1kepx47" href="#restart-a-self-managed-sharded-cluster" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="restart-a-self-managed-sharded-cluster">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#before-you-begin" target="_self">
<span>
Before You Begin
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#disable-the-balancer" target="_self">
<span>
Disable the Balancer
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#stop-sharded-cluster" target="_self">
<span>
Stop Sharded Cluster
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#start-sharded-cluster" target="_self">
<span>
Start Sharded Cluster
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#re-enable-the-balancer" target="_self">
<span>
Re-Enable the Balancer
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#validate-cluster-accessibility" target="_self">
<span>
Validate Cluster Accessibility
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
The tutorial is specific to MongoDB 8.0. For earlier
versions of MongoDB, refer to the corresponding version of the MongoDB
Manual.
</p>
<p class="leafygreen-ui-1kp3ins">
This procedure demonstrates the shutdown and startup sequence for restarting
a sharded cluster. Stopping or starting the components of a sharded cluster
in a different order may cause communication errors between members. For
example,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-shards/#std-label-shards-concepts">
shard
</a>
servers may appear to hang
if there are no
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-config-servers/#std-label-sharding-config-server">
config servers
</a>
available.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This procedure should only be performed during a planned
maintenance period. During this period, applications should stop
all reads and writes to the cluster in order to prevent potential
data loss or reading stale data.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Before You Begin
<a class="headerlink leafygreen-ui-1kepx47" href="#before-you-begin" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="before-you-begin">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, you can use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-directShardOperations">
<code class="leafygreen-ui-1l06pbn">
directShardOperations
</code>
</a>
role to perform maintenance operations
that require you to execute commands directly against a shard.
</p>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Running commands using the
<code class="leafygreen-ui-1l06pbn">
directShardOperations
</code>
role can cause
your cluster to stop working correctly and may cause data corruption.
Only use the
<code class="leafygreen-ui-1l06pbn">
directShardOperations
</code>
role for maintenance purposes
or under the guidance of MongoDB support. Once you are done
performing maintenance operations, stop using the
<code class="leafygreen-ui-1l06pbn">
directShardOperations
</code>
role.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-shutdown-sharded-cluster">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Disable the Balancer
<a class="headerlink leafygreen-ui-1kepx47" href="#disable-the-balancer" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="disable-the-balancer">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Disable the balancer to stop
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing">
chunk migration
</a>
and do not perform any metadata
write operations until the process finishes. If a migration is in
progress, the balancer will complete the in-progress migration before
stopping.
</p>
<p class="leafygreen-ui-1kp3ins">
To disable the balancer, connect to one of the cluster's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances and issue the following command:
<a class="footnote-reference header-buffer" href="#footnote-autosplit-stop" id="ref-autosplit-stop-id1">
[
<!-- -->
1
<!-- -->
]
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.stopBalancer()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">stopBalancer</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To check the balancer state, issue the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.getBalancerState/#mongodb-method-sh.getBalancerState">
<code class="leafygreen-ui-1l06pbn">
sh.getBalancerState()
</code>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-sharding-balancing-disable-temporarily">
Disable the Balancer
<!-- -->
.
</a>
</p>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-autosplit-stop" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-autosplit-stop-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
Starting in MongoDB 6.0.3, automatic chunk splitting is not performed.
This is because of balancing policy improvements. Auto-splitting commands
still exist, but do not perform an operation.
<!-- -->
In MongoDB versions earlier than 6.0.3,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.stopBalancer/#mongodb-method-sh.stopBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.stopBalancer()
</code>
</a>
also disables auto-splitting for the sharded cluster.
</td>
</tr>
</tbody>
</table>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Stop Sharded Cluster
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-sharded-cluster" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stop-sharded-cluster">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Stop mongos routers.Run db.shutdownServer() from the admin database on each\nmongos router:"},{"@type":"HowToStep","text":"Stop each shard replica set.Run db.shutdownServer() from the admin database on each\nshard replica set member to shutdown\nits mongod process. Shutdown all secondary members\nbefore shutting down the primary in each replica set."},{"@type":"HowToStep","text":"Stop config servers.Run db.shutdownServer() from the admin database on each\nof the config servers to\nshutdown its mongod process. Shutdown all\nsecondary members before shutting down the primary."}],"name":"Stop Sharded Cluster","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stop
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
routers.
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-mongos-routers" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stop-mongos-routers">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.shutdownServer/#mongodb-method-db.shutdownServer">
<code class="leafygreen-ui-1l06pbn">
db.shutdownServer()
</code>
</a>
from the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database on each
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
router:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use admin\ndb.shutdownServer()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use admin</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">shutdownServer</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stop each shard replica set.
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-each-shard-replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stop-each-shard-replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.shutdownServer/#mongodb-method-db.shutdownServer">
<code class="leafygreen-ui-1l06pbn">
db.shutdownServer()
</code>
</a>
from the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database on each
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-shards/#std-label-shards-concepts">
shard
</a>
replica set member to shutdown
its
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
process. Shutdown all secondary members
before shutting down the primary in each replica set.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stop config servers.
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-config-servers" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stop-config-servers">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.shutdownServer/#mongodb-method-db.shutdownServer">
<code class="leafygreen-ui-1l06pbn">
db.shutdownServer()
</code>
</a>
from the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database on each
of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-config-servers/#std-label-sharding-config-server">
config servers
</a>
to
shutdown its
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
process. Shutdown all
secondary members before shutting down the primary.
</p>
</section>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-start-sharded-cluster">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Start Sharded Cluster
<a class="headerlink leafygreen-ui-1kepx47" href="#start-sharded-cluster" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-sharded-cluster">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Start config servers.When starting each mongod, specify the\nmongod settings using either a configuration file or the\ncommand line. For more information on startup parameters, see the\nmongod reference page.Configuration FileIf using a configuration file, start the mongod with\nthe --config option set to the configuration file path.Command LineIf using the command line options, start the mongod\nwith the --configsvr, --replSet, --bind_ip,\nand other options as appropriate to your deployment. For example:After starting all config servers, connect to the primary\nmongod and run rs.status() to confirm the\nhealth and availability of each CSRS member."},{"@type":"HowToStep","text":"Start each shard replica set.When starting each mongod, specify the\nmongod settings using either a configuration file or the\ncommand line.Configuration FileIf using a configuration file, start the mongod with\nthe --config option set to the configuration file path.Command LineIf using the command line option, start the mongod with\nthe --replSet, --shardsvr, and --bind_ip options,\nand other options as appropriate to your deployment. For example:After starting all members of each shard, connect to each primary\nmongod and run rs.status() to confirm the health\nand availability of each member."},{"@type":"HowToStep","text":"Start mongos routers.Start mongos routers using either a configuration file or a\ncommand line parameter to specify the config servers.Configuration FileIf using a configuration file, start the mongos specifying\nthe --config option and the path to the configuration file.For more information on the configuration file, see\nconfiguration options.Command LineIf using command line parameters, start the mongos and specify\nthe --configdb, --bind_ip,\nand other options as appropriate to your deployment. For example:Before you bind your instance to a publicly-accessible IP address,\nyou must secure your cluster from unauthorized access. For a complete\nlist of security recommendations, see\nSecurity Checklist for Self-Managed Deployments. At minimum, consider\nenabling authentication and hardening\nnetwork infrastructure.Include any other options as appropriate for your deployment."}],"name":"Start Sharded Cluster","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start config servers.
<a class="headerlink leafygreen-ui-1kepx47" href="#start-config-servers" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-config-servers">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When starting each
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
, specify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
settings using either a configuration file or the
command line. For more information on startup parameters, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
reference page.
</p>
<p class="leafygreen-ui-1kp3ins">
<strong>
Configuration File
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
If using a configuration file, start the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
with
the
<code class="leafygreen-ui-1l06pbn">
--config
</code>
option set to the configuration file path.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --config <path-to-config-file>\n","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --config <path-to-config-file></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<strong>
Command Line
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
If using the command line options, start the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
with the
<code class="leafygreen-ui-1l06pbn">
--configsvr
</code>
,
<code class="leafygreen-ui-1l06pbn">
--replSet
</code>
,
<code class="leafygreen-ui-1l06pbn">
--bind_ip
</code>
,
and other options as appropriate to your deployment. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --configsvr --replSet <replica> --dbpath <path> --bind_ip localhost,<hostname(s)|ip>\n","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --configsvr --replSet <replica set name> --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
After starting all config servers, connect to the primary
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
and run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.status/#mongodb-method-rs.status">
<code class="leafygreen-ui-1l06pbn">
rs.status()
</code>
</a>
to confirm the
health and availability of each CSRS member.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start each shard replica set.
<a class="headerlink leafygreen-ui-1kepx47" href="#start-each-shard-replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-each-shard-replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When starting each
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
, specify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
settings using either a configuration file or the
command line.
</p>
<p class="leafygreen-ui-1kp3ins">
<strong>
Configuration File
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
If using a configuration file, start the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
with
the
<code class="leafygreen-ui-1l06pbn">
--config
</code>
option set to the configuration file path.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --config <path-to-config-file>\n","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --config <path-to-config-file></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<strong>
Command Line
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
If using the command line option, start the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
with
the
<code class="leafygreen-ui-1l06pbn">
--replSet
</code>
,
<code class="leafygreen-ui-1l06pbn">
--shardsvr
</code>
, and
<code class="leafygreen-ui-1l06pbn">
--bind_ip
</code>
options,
and other options as appropriate to your deployment. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --shardsvr --replSet <replsetname> --dbpath <path> --bind_ip localhost,<hostname(s)|ip>\n","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --shardsvr --replSet <replSetname> --dbpath <path> --bind_ip localhost,<hostname(s)|ip address(es)></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
After starting all members of each shard, connect to each primary
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
and run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.status/#mongodb-method-rs.status">
<code class="leafygreen-ui-1l06pbn">
rs.status()
</code>
</a>
to confirm the health
and availability of each member.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
routers.
<a class="headerlink leafygreen-ui-1kepx47" href="#start-mongos-routers" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-mongos-routers">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Start
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
routers using either a configuration file or a
command line parameter to specify the config servers.
</p>
<p class="leafygreen-ui-1kp3ins">
<strong>
Configuration File
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
If using a configuration file, start the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
specifying
the
<code class="leafygreen-ui-1l06pbn">
--config
</code>
option and the path to the configuration file.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongos --config <path-to-config>\n","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongos --config <path-to-config></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on the configuration file, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#std-label-configuration-options">
configuration options
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<strong>
Command Line
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
If using command line parameters, start the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
and specify
the
<code class="leafygreen-ui-1l06pbn">
--configdb
</code>
,
<code class="leafygreen-ui-1l06pbn">
--bind_ip
</code>
,
and other options as appropriate to your deployment. For example:
</p>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before you bind your instance to a publicly-accessible IP address,
you must secure your cluster from unauthorized access. For a complete
list of security recommendations, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-security-checklist">
Security Checklist for Self-Managed Deployments
</a>
. At minimum, consider
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-checklist-auth">
enabling authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-hardening/#std-label-network-config-hardening">
hardening
network infrastructure
<!-- -->
.
</a>
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongos --configdb <configreplsetname>/cfg1.example.net:27019,cfg2.example.net:27019 --bind_ip localhost,<hostname(s)|ip>\n","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongos --configdb <configReplSetName>/cfg1.example.net:27019,cfg2.example.net:27019 --bind_ip localhost,<hostname(s)|ip address(es)></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Include any other options as appropriate for your deployment.
</p>
</section>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Re-Enable the Balancer
<a class="headerlink leafygreen-ui-1kepx47" href="#re-enable-the-balancer" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="re-enable-the-balancer">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Re-enable the balancer to resume
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing">
chunk migrations
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Connect to one of the cluster's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances and run
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.startBalancer/#mongodb-method-sh.startBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.startBalancer()
</code>
</a>
command:
<a class="footnote-reference header-buffer" href="#footnote-autosplit-start" id="ref-autosplit-start-id2">
[
<!-- -->
2
<!-- -->
]
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.startBalancer()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">startBalancer</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To check the balancer state, issue the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.getBalancerState/#mongodb-method-sh.getBalancerState">
<code class="leafygreen-ui-1l06pbn">
sh.getBalancerState()
</code>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-sharding-balancing-enable">
Enable the Balancer
<!-- -->
.
</a>
</p>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-autosplit-start" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-autosplit-start-id2">
2
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
Starting in MongoDB 6.0.3, automatic chunk splitting is not performed.
This is because of balancing policy improvements. Auto-splitting commands
still exist, but do not perform an operation.
<!-- -->
In MongoDB versions earlier than 6.0.3,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.startBalancer/#mongodb-method-sh.startBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.startBalancer()
</code>
</a>
also enables auto-splitting for the sharded cluster.
</td>
</tr>
</tbody>
</table>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Validate Cluster Accessibility
<a class="headerlink leafygreen-ui-1kepx47" href="#validate-cluster-accessibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="validate-cluster-accessibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Connect a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongo/#mongodb-binary-bin.mongo">
<code class="leafygreen-ui-1l06pbn">
mongo
</code>
</a>
shell to one of the cluster's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
processes. Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-method-sh.status">
<code class="leafygreen-ui-1l06pbn">
sh.status()
</code>
</a>
to check the overall cluster status.
</p>
<p class="leafygreen-ui-1kp3ins">
To confirm that all shards are accessible and communicating, insert
test data into a temporary sharded collection. Confirm that data is
being split and migrated between each shard in your cluster. You can
connect a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongo/#mongodb-binary-bin.mongo">
<code class="leafygreen-ui-1l06pbn">
mongo
</code>
</a>
shell to each shard primary and
use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
to validate that the data was
sharded as expected.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
To prevent potential data loss or reading stale data,
do not start application reads and writes to the cluster
until after confirming the cluster is healthy and accessible.
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/replace-config-server/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Replace a Self-Managed Config Server
<a class="headerlink leafygreen-ui-1kepx47" href="#replace-a-self-managed-config-server" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="replace-a-self-managed-config-server">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#overview" target="_self">
<span>
Overview
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#considerations" target="_self">
<span>
Considerations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The following procedure applies to 8.0 config
servers. For earlier versions of MongoDB, refer to the corresponding
version of the MongoDB Manual.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Overview
<a class="headerlink leafygreen-ui-1kepx47" href="#overview" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="overview">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If the config server replica set becomes read only, i.e. does not
have a primary, the sharded cluster cannot support operations that change
the cluster metadata, such as chunk splits and migrations. Although no
chunks can be split or migrated, applications will be able to write data
to the sharded cluster.
</p>
<p class="leafygreen-ui-1kp3ins">
If one of the config servers is unavailable or inoperable, repair or
replace it as soon as possible. The following procedure replaces a member
of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-config-servers/#std-label-sharding-config-server">
config server replica set
</a>
with a new
member.
</p>
<p class="leafygreen-ui-1kp3ins">
The tutorial is specific to MongoDB 8.0. For earlier
versions of MongoDB, refer to the corresponding version of the MongoDB
Manual.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="considerations">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following restrictions apply to a replica set configuration when used
for config servers:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Must have zero
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-arbiter/#std-label-replica-set-arbiter-configuration">
arbiters
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Must have no
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-delayed-member/">
delayed members
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Must build indexes (i.e. no member should have
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.buildIndexes">
<code class="leafygreen-ui-1l06pbn">
members[n].buildIndexes
</code>
</a>
setting set to false).
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Start the replacement config server.Start a mongod instance, specifying the --configsvr,\n--replSet, --bind_ip options, and other options as\nappropriate to your deployment.Before you bind your instance to a publicly-accessible IP address,\nyou must secure your cluster from unauthorized access. For a complete\nlist of security recommendations, see\nSecurity Checklist for Self-Managed Deployments. At minimum, consider\nenabling authentication and hardening\nnetwork infrastructure."},{"@type":"HowToStep","text":"Add the new config server to the replica set.Connect mongosh to the primary of the config server\nreplica set and use rs.add() to add the new member.Before MongoDB 5.0, a newly added secondary still counts as a voting\nmember even though it can neither serve reads nor become primary until\nits data is consistent. If you are running a MongoDB version earlier\nthan 5.0 and add a secondary with its votes\nand priority settings greater than zero, this can\nlead to a case where a majority of the voting members are\nonline but no primary can be elected. To avoid such situations,\nconsider adding the new secondary initially with\npriority :0 and votes :0. Then, run rs.status() to ensure the\nmember has transitioned into SECONDARY state. Finally, use\nrs.reconfig() to update its priority and votes.The initial sync process copies all the data from one member of the\nconfig server replica set to the new member without restarting.mongos instances automatically recognize the change in the\nconfig server replica set members without restarting."},{"@type":"HowToStep","text":"Update the newly added config server's votes and priority settings.Ensure that the new member has reached SECONDARY\nstate. To check the state of the replica set members, run\nrs.status():Reconfigure the replica set to update the votes and priority of\nthe new member:where n is the array index of the new member in the\nmembers array.The rs.reconfig() shell method can force the current\nprimary to step down, which causes an election. When the primary steps down, the\nmongod closes all client connections. While this\ntypically takes 10-20 seconds, try to make these changes during\nscheduled maintenance periods.Avoid reconfiguring replica sets that contain members of different\nMongoDB versions as validation rules may differ across MongoDB versions."},{"@type":"HowToStep","text":"Shut down the member to replace.If replacing the primary member, step down the primary first before\nshutting down."},{"@type":"HowToStep","text":"Remove the member to replace from the config server replica set.Upon completion of initial sync of the replacement config server,\nfrom a mongosh session that is connected to the\nprimary, use rs.remove() to remove the old member.mongos instances automatically recognize the change in the\nconfig server replica set members without restarting.end-for-migration-steps"},{"@type":"HowToStep","text":"If necessary, update mongos configuration or DNS entry.With replica set config servers, the mongos instances specify\nin the --configdb or sharding.configDB setting the config\nserver replica set name and at least one of the replica set members.As such, if the mongos instance does not specify the\nremoved replica set member in the --configdb or\nsharding.configDB setting, no further action is necessary.If, however, a mongos instance specified the removed\nmember in the --configdb or configDB\nsetting, either:Update the setting for the next time you restart the\nmongos, orModify the DNS entry that points to the system that provided the old\nconfig server, so that the same hostname points to the new\nconfig server."}],"name":"Procedure","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start the replacement config server.
<a class="headerlink leafygreen-ui-1kepx47" href="#start-the-replacement-config-server" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-the-replacement-config-server">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Start a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance, specifying the
<code class="leafygreen-ui-1l06pbn">
--configsvr
</code>
,
<code class="leafygreen-ui-1l06pbn">
--replSet
</code>
,
<code class="leafygreen-ui-1l06pbn">
--bind_ip
</code>
options, and other options as
appropriate to your deployment.
</p>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before you bind your instance to a publicly-accessible IP address,
you must secure your cluster from unauthorized access. For a complete
list of security recommendations, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-security-checklist">
Security Checklist for Self-Managed Deployments
</a>
. At minimum, consider
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-checklist-auth">
enabling authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-hardening/#std-label-network-config-hardening">
hardening
network infrastructure
<!-- -->
.
</a>
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --configsvr --replSet <replicasetname> --bind_ip localhost,<hostname(s)|ip>\n","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --configsvr --replSet <replicaSetName> --bind_ip localhost,<hostname(s)|ip address(es)></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Add the new config server to the replica set.
<a class="headerlink leafygreen-ui-1kepx47" href="#add-the-new-config-server-to-the-replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="add-the-new-config-server-to-the-replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Connect
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
to the primary of the config server
replica set and use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.add/#mongodb-method-rs.add">
<code class="leafygreen-ui-1l06pbn">
rs.add()
</code>
</a>
to add the new member.
</p>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before MongoDB 5.0, a newly added secondary still counts as a voting
member even though it can neither serve reads nor become primary until
its data is consistent. If you are running a MongoDB version earlier
than 5.0 and add a secondary with its
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.votes">
<code class="leafygreen-ui-1l06pbn">
votes
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.priority">
<code class="leafygreen-ui-1l06pbn">
priority
</code>
</a>
settings greater than zero, this can
lead to a case where a majority of the voting members are
online but no primary can be elected. To avoid such situations,
consider adding the new secondary initially with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.priority">
<code class="leafygreen-ui-1l06pbn">
priority :0
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.votes">
<code class="leafygreen-ui-1l06pbn">
votes :0
</code>
</a>
. Then, run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.status/#mongodb-method-rs.status">
<code class="leafygreen-ui-1l06pbn">
rs.status()
</code>
</a>
to ensure the
member has transitioned into
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.SECONDARY">
<code class="leafygreen-ui-1l06pbn">
SECONDARY
</code>
</a>
state. Finally, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#mongodb-method-rs.reconfig">
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
</a>
to update its priority and votes.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.add( { host: \"<hostnamenew>:<portnew>\", priority: 0, votes: 0 } )\n","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">add</span>( <!-- -->{ <span class="lg-highlight-attr">host</span>: <span class="lg-highlight-string">"<hostnameNew>:<portNew>"</span>, <span class="lg-highlight-attr">priority</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">votes</span>: <span class="lg-highlight-number">0</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The initial sync process copies all the data from one member of the
config server replica set to the new member without restarting.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances automatically recognize the change in the
config server replica set members without restarting.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update the newly added config server's votes and priority settings.
<a class="headerlink leafygreen-ui-1kepx47" href="#update-the-newly-added-config-server-s-votes-and-priority-settings" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="update-the-newly-added-config-server-s-votes-and-priority-settings">
</div>
</a>
</h3>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure that the new member has reached
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.SECONDARY">
<code class="leafygreen-ui-1l06pbn">
SECONDARY
</code>
</a>
state. To check the state of the replica set members, run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.status/#mongodb-method-rs.status">
<code class="leafygreen-ui-1l06pbn">
rs.status()
</code>
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.status()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">status</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reconfigure the replica set to update the votes and priority of
the new member:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var cfg = rs.conf();\n\ncfg.members[n].priority = 1; // Substitute the correct array index for the new member\ncfg.members[n].votes = 1; // Substitute the correct array index for the new member\n\nrs.reconfig(cfg)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> cfg <!-- -->= <!-- -->rs.<span class="lg-highlight-title lg-highlight-function">conf</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">cfg.<span class="lg-highlight-property">members</span>[<!-- -->n].<span class="lg-highlight-property">priority</span> <!-- -->= <span class="lg-highlight-number">1</span>; <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Substitute the correct </span><span class="lg-highlight-comment">array</span><span class="lg-highlight-comment"> index for the </span><span class="lg-highlight-comment">new member</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">cfg.<span class="lg-highlight-property">members</span>[<!-- -->n].<span class="lg-highlight-property">votes</span> <!-- -->= <span class="lg-highlight-number">1</span>; <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Substitute the correct </span><span class="lg-highlight-comment">array</span><span class="lg-highlight-comment"> index for the </span><span class="lg-highlight-comment">new member</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">reconfig</span>(<!-- -->cfg)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
where
<code class="leafygreen-ui-1l06pbn">
n
</code>
is the array index of the new member in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members">
<code class="leafygreen-ui-1l06pbn">
members
</code>
</a>
array.
</p>
</li>
</ol>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#mongodb-method-rs.reconfig">
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
</a>
shell method can force the current
primary to step down, which causes an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-elections/#std-label-replica-set-elections">
election
</a>
. When the primary steps down, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
closes all client connections. While this
typically takes 10-20 seconds, try to make these changes during
scheduled maintenance periods.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Avoid reconfiguring replica sets that contain members of different
MongoDB versions as validation rules may differ across MongoDB versions.
</p>
</li>
</ul>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
4
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Shut down the member to replace.
<a class="headerlink leafygreen-ui-1kepx47" href="#shut-down-the-member-to-replace" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="shut-down-the-member-to-replace">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If replacing the primary member, step down the primary first before
shutting down.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
5
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Remove the member to replace from the config server replica set.
<a class="headerlink leafygreen-ui-1kepx47" href="#remove-the-member-to-replace-from-the-config-server-replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="remove-the-member-to-replace-from-the-config-server-replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Upon completion of initial sync of the replacement config server,
from a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
session that is connected to the
primary, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.remove/#mongodb-method-rs.remove">
<code class="leafygreen-ui-1l06pbn">
rs.remove()
</code>
</a>
to remove the old member.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.remove(\"<hostnameold>:<portold>\")\n","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">remove</span>(<span class="lg-highlight-string">"<hostnameOld>:<portOld>"</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances automatically recognize the change in the
config server replica set members without restarting.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
6
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
If necessary, update
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
configuration or DNS entry.
<a class="headerlink leafygreen-ui-1kepx47" href="#if-necessary-update-mongos-configuration-or-dns-entry" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="if-necessary-update-mongos-configuration-or-dns-entry">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
With replica set config servers, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances specify
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-option-mongos.--configdb">
<code class="leafygreen-ui-1l06pbn">
--configdb
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-sharding.configDB">
<code class="leafygreen-ui-1l06pbn">
sharding.configDB
</code>
</a>
setting the config
server replica set name and at least one of the replica set members.
</p>
<p class="leafygreen-ui-1kp3ins">
As such, if the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance does not specify the
removed replica set member in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-option-mongos.--configdb">
<code class="leafygreen-ui-1l06pbn">
--configdb
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-sharding.configDB">
<code class="leafygreen-ui-1l06pbn">
sharding.configDB
</code>
</a>
setting, no further action is necessary.
</p>
<p class="leafygreen-ui-1kp3ins">
If, however, a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance specified the removed
member in the
<code class="leafygreen-ui-1l06pbn">
--configdb
</code>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-sharding.configDB">
<code class="leafygreen-ui-1l06pbn">
configDB
</code>
</a>
setting, either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update the setting for the next time you restart the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
, or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Modify the DNS entry that points to the system that provided the old
config server, so that the
<em>
same
</em>
hostname points to the new
config server.
</p>
</li>
</ul>
</section>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Binary.createFromHexString/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Binary.createFromHexString()
<a class="headerlink leafygreen-ui-1kepx47" href="#binary.createfromhexstring--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="binary.createfromhexstring--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Creates a binary object from a hexadecimal value.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
Binary.createFromHexString( <hexadecimalString> )
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Binary.createFromHexString" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-Binary.createFromHexString">
</div>
</a>
</dt>
<dd>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
hexadecimalString
</code>
field specifies a string that contains a
hexadecimal value. For example,
<code class="leafygreen-ui-1l06pbn">
"64c13ab08edf48a008793cac"
</code>
.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following examples show how to add a binary object to a document
using
<code class="leafygreen-ui-1l06pbn">
Binary.createFromHexString()
</code>
and how the binary object appears
in the output when retrieved.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create Collection Containing Document with Binary Object
<a class="headerlink leafygreen-ui-1kepx47" href="#create-collection-containing-document-with-binary-object" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-collection-containing-document-with-binary-object">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example creates a collection named
<code class="leafygreen-ui-1l06pbn">
binaryObjectsFromHexString
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.binaryObjectsFromHexString.insertOne( {\n _id: 0,\n binaryObject: Binary.createFromHexString( \"64c13ab08edf48a008793cac\" )\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">binaryObjectsFromHexString</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">binaryObject</span>: <span class="lg-highlight-title lg-highlight-class">Binary</span>.<span class="lg-highlight-title lg-highlight-function">createFromHexString</span>( <span class="lg-highlight-string">"64c13ab08edf48a008793cac"</span> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
binaryObject
</code>
field contains the binary object created from the
string specified in
<code class="leafygreen-ui-1l06pbn">
Binary.createFromHexString()
</code>
.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Retrieve Document from Collection with Binary Object
<a class="headerlink leafygreen-ui-1kepx47" href="#retrieve-document-from-collection-with-binary-object" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="retrieve-document-from-collection-with-binary-object">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example retrieves the document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.binaryObjectsFromHexString.findOne( { _id: 0 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">binaryObjectsFromHexString</span>.<span class="lg-highlight-title lg-highlight-function">findOne</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
2.0.0, binary values are shown as
<code class="leafygreen-ui-1l06pbn">
Binary.createFromBase64( <base64String> )
</code>
values instead of
<code class="leafygreen-ui-1l06pbn">
Binary( Buffer.from( <base64String> ) )
</code>
values. This only changes
the display of binary values.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Example output, which shows the number in base64:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 0,\n binaryObject: Binary.createFromBase64(\"ZME6sI7fSKAIeTys\")\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">binaryObject</span>: <span class="lg-highlight-title lg-highlight-class">Binary</span>.<span class="lg-highlight-title lg-highlight-function">createFromBase64</span>(<span class="lg-highlight-string">"ZME6sI7fSKAIeTys"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sh.waitForPingChange/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.waitForPingChange()
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.waitforpingchange--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sh.waitforpingchange--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
sh.waitForPingChange(activePings, timeout, interval)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.waitForPingChange" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-sh.waitForPingChange">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.waitForPingChange" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.waitForPingChange()
</code>
</span>
</a>
waits for a change in ping state
of one of the
<code class="leafygreen-ui-1l06pbn">
activepings
</code>
, and only returns when the specified
ping changes state.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
activePings
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
An array of active pings from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
collection.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
timeout
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of milliseconds to wait for a change in ping state.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
interval
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Number of milliseconds to sleep in each waiting cycle.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/WriteResult.hasWriteError/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
WriteResult.hasWriteError()
<a class="headerlink leafygreen-ui-1kepx47" href="#writeresult.haswriteerror--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="writeresult.haswriteerror--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
WriteResult.hasWriteError()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-WriteResult.hasWriteError" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-WriteResult.hasWriteError">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the result of a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
write
method has
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/WriteResult/#mongodb-data-WriteResult.writeError">
<code class="leafygreen-ui-1l06pbn">
WriteResult.writeError
</code>
</a>
. Otherwise, the method
returns
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</dd>
</dl>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/WriteResult/#mongodb-method-WriteResult">
<code class="leafygreen-ui-1l06pbn">
WriteResult()
</code>
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/locf/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$locf (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-locf--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-locf--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="group">
<dt>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-group-grp.-locf" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-group-grp.-locf">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Last observation carried forward. Sets values for
<code class="leafygreen-ui-1l06pbn">
null
</code>
and missing
fields in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-window">
window
</a>
to the last non-null
value for the field.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
is only available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stage.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
expression has this syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $locf: <expression> }"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ $locf: <expression> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-locf-behavior">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If a field being filled contains both
<code class="leafygreen-ui-1l06pbn">
null
</code>
and non-null values,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
sets the
<code class="leafygreen-ui-1l06pbn">
null
</code>
and missing values to the field's
last known non-null value according to the sort order specified in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
null
</code>
and missing field values that appear before non-null values
in the sort order remain
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If a field being filled contains only
<code class="leafygreen-ui-1l06pbn">
null
</code>
or missing values in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
partition
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
sets the
field value to
<code class="leafygreen-ui-1l06pbn">
null
</code>
for that partition.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Comparison of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/fill/#mongodb-pipeline-pipe.-fill">
<code class="leafygreen-ui-1l06pbn">
$fill
</code>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
<a class="headerlink leafygreen-ui-1kepx47" href="#comparison-of--and" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="comparison-of--and">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To fill missing field values based on the last observed value in a
sequence, you can use:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/fill/#mongodb-pipeline-pipe.-fill">
<code class="leafygreen-ui-1l06pbn">
$fill
</code>
</a>
stage with
<code class="leafygreen-ui-1l06pbn">
{ method: "locf" }
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
When you use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/fill/#mongodb-pipeline-pipe.-fill">
<code class="leafygreen-ui-1l06pbn">
$fill
</code>
</a>
stage, the field you specify in the
output is the same field used as the source data. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/fill/#std-label-fill-example-locf">
Fill Missing Field Values Based on the Last Observed Value
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
operator inside of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stage.
</p>
<p class="leafygreen-ui-1kp3ins">
When you use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
operator, you can set values
for a different field than the field used as the source data. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-locf-example-multiple-methods" target="_self">
<span>
Use Multiple Fill Methods in a Single Stage
<!-- -->
.
</span>
</a>
</p>
</li>
</ul>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The examples on this page use a
<code class="leafygreen-ui-1l06pbn">
stock
</code>
collection that contains
tracks a single company's stock price at hourly intervals:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stock.insertMany( [\n {\n time: ISODate(\"2021-03-08T09:00:00.000Z\"),\n price: 500\n },\n {\n time: ISODate(\"2021-03-08T10:00:00.000Z\"),\n },\n {\n time: ISODate(\"2021-03-08T11:00:00.000Z\"),\n price: 515\n },\n {\n time: ISODate(\"2021-03-08T12:00:00.000Z\")\n },\n {\n time: ISODate(\"2021-03-08T13:00:00.000Z\")\n },\n {\n time: ISODate(\"2021-03-08T14:00:00.000Z\"),\n price: 485\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stock</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T09:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T10:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T11:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T12:00:00.000Z"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T13:00:00.000Z"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T14:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">485</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
price
</code>
field is missing for some of the documents in the
collection.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Fill Missing Values with the Last Observed Value
<a class="headerlink leafygreen-ui-1kepx47" href="#fill-missing-values-with-the-last-observed-value" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="fill-missing-values-with-the-last-observed-value">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
operator to set missing
fields to the value from the last-observed non-
<code class="leafygreen-ui-1l06pbn">
null
</code>
value:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stock.aggregate( [\n {\n $setWindowFields: {\n sortBy: { time: 1 },\n output: {\n price: { $locf: \"$price\" }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stock</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$setWindowFields</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">output</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <!-- -->{ <span class="lg-highlight-attr">$locf</span>: <span class="lg-highlight-string">"$price"</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sortBy: { time: 1 }
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sorts
</a>
the documents in each partition by
<code class="leafygreen-ui-1l06pbn">
time
</code>
in ascending order (
<code class="leafygreen-ui-1l06pbn">
1
</code>
), so the earliest
<code class="leafygreen-ui-1l06pbn">
time
</code>
is first.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For documents where the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field is missing, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
operator sets the
<code class="leafygreen-ui-1l06pbn">
price
</code>
to the last-observed value in the
sequence.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId(\"62169b65394d47411658b5f5\"),\n time: ISODate(\"2021-03-08T09:00:00.000Z\"),\n price: 500\n },\n {\n _id: ObjectId(\"62169b65394d47411658b5f6\"),\n time: ISODate(\"2021-03-08T10:00:00.000Z\"),\n price: 500\n },\n {\n _id: ObjectId(\"62169b65394d47411658b5f7\"),\n time: ISODate(\"2021-03-08T11:00:00.000Z\"),\n price: 515\n },\n {\n _id: ObjectId(\"62169b65394d47411658b5f8\"),\n time: ISODate(\"2021-03-08T12:00:00.000Z\"),\n price: 515\n },\n {\n _id: ObjectId(\"62169b65394d47411658b5f9\"),\n time: ISODate(\"2021-03-08T13:00:00.000Z\"),\n price: 515\n },\n {\n _id: ObjectId(\"62169b65394d47411658b5fa\"),\n time: ISODate(\"2021-03-08T14:00:00.000Z\"),\n price: 485\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"62169b65394d47411658b5f5"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T09:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"62169b65394d47411658b5f6"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T10:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"62169b65394d47411658b5f7"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T11:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"62169b65394d47411658b5f8"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T12:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"62169b65394d47411658b5f9"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T13:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"62169b65394d47411658b5fa"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T14:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">485</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-locf-example-multiple-methods">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use Multiple Fill Methods in a Single Stage
<a class="headerlink leafygreen-ui-1kepx47" href="#use-multiple-fill-methods-in-a-single-stage" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="use-multiple-fill-methods-in-a-single-stage">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When you use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stage to fill missing
values, you can set values for a different field than the field you
fill from. As a result, you can use multiple fill methods in a single
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stage and output the results in distinct
fields.
</p>
<p class="leafygreen-ui-1kp3ins">
The following pipeline populates missing
<code class="leafygreen-ui-1l06pbn">
price
</code>
fields using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Linear_interpolation" rel="noopener noreferrer">
<span>
linear interpolation
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
and the last-observation-carried-forward method:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stock.aggregate( [\n {\n $setWindowFields:\n {\n sortBy: { time: 1 },\n output:\n {\n linearFillPrice: { $linearFill: \"$price\" },\n locfPrice: { $locf: \"$price\" }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stock</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$setWindowFields</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">output</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">linearFillPrice</span>: <!-- -->{ <span class="lg-highlight-attr">$linearFill</span>: <span class="lg-highlight-string">"$price"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">locfPrice</span>: <!-- -->{ <span class="lg-highlight-attr">$locf</span>: <span class="lg-highlight-string">"$price"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sortBy: { time: 1 }
</code>
sorts the documents by the
<code class="leafygreen-ui-1l06pbn">
time
</code>
field in
ascending order, from earliest to latest.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-output">
output
</a>
specifies:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
linearFillPrice
</code>
as a target field to be filled.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ $linearFill: "$price" }
</code>
is the value for the
<code class="leafygreen-ui-1l06pbn">
linearFillPrice
</code>
field.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/linearFill/#mongodb-group-grp.-linearFill">
<code class="leafygreen-ui-1l06pbn">
$linearFill
</code>
</a>
fills missing
<code class="leafygreen-ui-1l06pbn">
price
</code>
values using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Linear_interpolation" rel="noopener noreferrer">
<span>
linear interpolation
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
based on the
surrounding
<code class="leafygreen-ui-1l06pbn">
price
</code>
values in the sequence.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
locfPrice
</code>
as a target field to be filled.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ $locf: "$price" }
</code>
is the value for the
<code class="leafygreen-ui-1l06pbn">
locfPrice
</code>
field.
<code class="leafygreen-ui-1l06pbn">
locf
</code>
stands for last observation carried forward.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-locf" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</span>
</a>
fills missing
<code class="leafygreen-ui-1l06pbn">
price
</code>
values with the value from
the previous document in the sequence.
</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId(\"620ad555394d47411658b5ef\"),\n time: ISODate(\"2021-03-08T09:00:00.000Z\"),\n price: 500,\n linearFillPrice: 500,\n locfPrice: 500\n },\n {\n _id: ObjectId(\"620ad555394d47411658b5f0\"),\n time: ISODate(\"2021-03-08T10:00:00.000Z\"),\n linearFillPrice: 507.5,\n locfPrice: 500\n },\n {\n _id: ObjectId(\"620ad555394d47411658b5f1\"),\n time: ISODate(\"2021-03-08T11:00:00.000Z\"),\n price: 515,\n linearFillPrice: 515,\n locfPrice: 515\n },\n {\n _id: ObjectId(\"620ad555394d47411658b5f2\"),\n time: ISODate(\"2021-03-08T12:00:00.000Z\"),\n linearFillPrice: 505,\n locfPrice: 515\n },\n {\n _id: ObjectId(\"620ad555394d47411658b5f3\"),\n time: ISODate(\"2021-03-08T13:00:00.000Z\"),\n linearFillPrice: 495,\n locfPrice: 515\n },\n {\n _id: ObjectId(\"620ad555394d47411658b5f4\"),\n time: ISODate(\"2021-03-08T14:00:00.000Z\"),\n price: 485,\n linearFillPrice: 485,\n locfPrice: 485\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5ef"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T09:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">500</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">500</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f0"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T10:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">507.5</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f1"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T11:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">515</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">515</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f2"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T12:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">505</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f3"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T13:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">495</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f4"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T14:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">485</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">485</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">485</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/cursor.allowPartialResults/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
cursor.allowPartialResults()
<a class="headerlink leafygreen-ui-1kepx47" href="#cursor.allowpartialresults--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="cursor.allowpartialresults--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
cursor.allowPartialResults()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-cursor.allowPartialResults" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-cursor.allowPartialResults">
</div>
</a>
</dt>
<dd>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for a language-specific driver, such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
When used with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
operations against a
sharded collection, returns partial results, rather than an error,
if one or more queried shards are unavailable.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.allowPartialResults" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.allowPartialResults()
</code>
</span>
</a>
method has the following
syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.find(<query>, <projection>).allowPartialResults()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<<!-- -->query<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->projection<!-- -->><!-- -->).<span class="lg-highlight-title lg-highlight-function">allowPartialResults</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/query/nin/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$nin
<a class="headerlink leafygreen-ui-1kepx47" href="#-nin" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-nin">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<dl class="query">
<dt>
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-query-op.-nin" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-query-op.-nin">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</span>
</a>
selects the documents where:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the specified field value is not in the specified array
<strong>
or
</strong>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the specified field does not exist.
</p>
</li>
</ul>
</dd>
</dl>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can use
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
for deployments hosted in the following
environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</span>
</a>
operator has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ field: { $nin: [ <value1>, <value2> ... <valuen> ] } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">field</span>: <!-- -->{ <span class="lg-highlight-attr">$nin</span>: <!-- -->[ <<!-- -->value1<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->value2<!-- -->> <!-- -->...<!-- --> <!-- --><<!-- -->valueN<!-- -->> <!-- -->] } }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
field
</code>
holds an array, then the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</span>
</a>
operator selects
the documents whose
<code class="leafygreen-ui-1l06pbn">
field
</code>
holds an array with
<strong>
no
</strong>
element equal to
a value in the specified array (for example,
<code class="leafygreen-ui-1l06pbn">
<value1>
</code>
,
<code class="leafygreen-ui-1l06pbn">
<value2>
</code>
, and so on).
</p>
<p class="leafygreen-ui-1kp3ins">
For comparison of different BSON type values, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order">
specified
BSON comparison order
<!-- -->
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Create the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.insertMany( [\n { \"item\": \"Pens\", \"quantity\": 350, \"tags\": [ \"school\", \"office\" ] },\n { \"item\": \"Erasers\", \"quantity\": 15, \"tags\": [ \"school\", \"home\" ] },\n { \"item\": \"Maps\", \"tags\": [ \"office\", \"storage\" ] },\n { \"item\": \"Books\", \"quantity\": 5, \"tags\": [ \"school\", \"storage\", \"home\" ] }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"item"</span>: <span class="lg-highlight-string">"Pens"</span>, <span class="lg-highlight-string">"quantity"</span>: <span class="lg-highlight-number">350</span>, <span class="lg-highlight-string">"tags"</span>: <!-- -->[ <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"office"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"item"</span>: <span class="lg-highlight-string">"Erasers"</span>, <span class="lg-highlight-string">"quantity"</span>: <span class="lg-highlight-number">15</span>, <span class="lg-highlight-string">"tags"</span>: <!-- -->[ <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"home"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"item"</span>: <span class="lg-highlight-string">"Maps"</span>, <span class="lg-highlight-string">"tags"</span>: <!-- -->[ <span class="lg-highlight-string">"office"</span>, <span class="lg-highlight-string">"storage"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"item"</span>: <span class="lg-highlight-string">"Books"</span>, <span class="lg-highlight-string">"quantity"</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"tags"</span>: <!-- -->[ <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"storage"</span>, <span class="lg-highlight-string">"home"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Select on Unmatching Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#select-on-unmatching-documents" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="select-on-unmatching-documents">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following query selects all documents from the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection where the
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
does
<strong>
not
</strong>
equal either 5 or 15.
</p>
<p class="leafygreen-ui-1kp3ins">
The query also matches documents that do
<em>
not
</em>
have a
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
field.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.find( { quantity: { $nin: [ 5, 15 ] } }, { _id: 0 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">quantity</span>: <!-- -->{ <span class="lg-highlight-attr">$nin</span>: <!-- -->[ <span class="lg-highlight-number">5</span>, <span class="lg-highlight-number">15</span> ] } }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ item: 'Pens', quantity: 350, tags: [ 'school', 'office' ] },\n{ item: 'Maps', tags: [ 'office', 'storage' ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">'Pens'</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">350</span>, <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">'school'</span>, <span class="lg-highlight-string">'office'</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">'Maps'</span>, <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">'office'</span>, <span class="lg-highlight-string">'storage'</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Select on Elements Not in an Array
<a class="headerlink leafygreen-ui-1kepx47" href="#select-on-elements-not-in-an-array" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="select-on-elements-not-in-an-array">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Set the
<code class="leafygreen-ui-1l06pbn">
exclude
</code>
field to
<code class="leafygreen-ui-1l06pbn">
true
</code>
for documents that don't have the
<code class="leafygreen-ui-1l06pbn">
"school"
</code>
tag.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.updateMany(\n { tags: { $nin: [ \"school\" ] } },\n { $set: { exclude: true } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">tags</span>: <!-- -->{ <span class="lg-highlight-attr">$nin</span>: <!-- -->[ <span class="lg-highlight-string">"school"</span> ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">exclude</span>: <span class="lg-highlight-literal">true</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateMany/#mongodb-method-db.collection.updateMany">
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</a>
also selects a document when the
document does not contain the field
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</span>
</a>
is matching on.
</p>
<p class="leafygreen-ui-1kp3ins">
The inequality operator
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</span>
</a>
is
<em>
not
</em>
very selective since
it often matches a large portion of the index. As a result, in many
cases, a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</span>
</a>
query with an index may perform no better
than a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</span>
</a>
query that must scan all documents in a
collection. See also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-optimization/#std-label-read-operations-query-selectivity">
Query Selectivity
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateMany/#mongodb-method-db.collection.updateMany">
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/set/#mongodb-update-up.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/system-roles-collection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
<code class="leafygreen-ui-1l06pbn">
system.roles
</code>
Collection in Self-Managed Deployments
<a class="headerlink leafygreen-ui-1kepx47" href="#system.roles-collection-in-self-managed-deployments" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="system.roles-collection-in-self-managed-deployments">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#system.roles-schema" target="_self">
<span>
<code class="css-1kekw28 e1wawog0">
system.roles
</code>
Schema
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
system.roles
</code>
collection in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database stores the
user-defined roles. To create and manage these user-defined
roles, MongoDB provides
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/#std-label-role-management-commands">
role management commands
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-admin-system-roles-collection">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
<code class="leafygreen-ui-1l06pbn">
system.roles
</code>
Schema
<a class="headerlink leafygreen-ui-1kepx47" href="#system.roles-schema" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="system.roles-schema">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The documents in the
<code class="leafygreen-ui-1l06pbn">
system.roles
</code>
collection have the following
schema:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: <system-defined>,\n role: \"<role>\",\n db: \"<database>\",\n privileges:\n [\n {\n resource: { <resource> },\n actions: [ \"<action>\", ... ]\n },\n ...\n ],\n roles:\n [\n { role: \"<role>\", db: \"<database>\" },\n ...\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <!-- --><system-defined id>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> role: "<role name>",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> db: "<database>",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> privileges:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> resource: { <resource> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> actions: [ "<action>", ... ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> roles:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { role: "<role name>", db: "<database>" },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
system.roles
</code>
document has the following fields:
</p>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
admin.system.roles.role
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-admin.system.roles.role" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-admin.system.roles.role">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-admin.system.roles.role" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
role
</code>
</span>
</a>
field is a string that
specifies the name of the role.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
admin.system.roles.db
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-admin.system.roles.db" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-admin.system.roles.db">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-admin.system.roles.db" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db
</code>
</span>
</a>
field is a string that specifies
the database to which the role belongs. MongoDB uniquely identifies
each role by the pairing of its name (i.e.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-admin.system.roles.role" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
role
</code>
</span>
</a>
) and its database.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
admin.system.roles.privileges
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-admin.system.roles.privileges" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-admin.system.roles.privileges">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-admin.system.roles.privileges" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
privileges
</code>
</span>
</a>
array contains the
privilege documents that define the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-privileges">
privileges
</a>
for the role.
</p>
<p class="leafygreen-ui-1kp3ins">
A privilege document has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n resource: { <resource> },\n actions: [ \"<action>\", ... ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">resource</span>: <!-- -->{ <<!-- -->resource<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"<action>"</span>, <!-- -->...<!-- --> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Each privilege document has the following fields:
</p>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
admin.system.roles.privileges[n].resource
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-admin.system.roles.privileges-n-.resource" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-admin.system.roles.privileges-n-.resource">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
A document that specifies the resources upon which the privilege
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-admin.system.roles.privileges-n-.actions" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
actions
</code>
</span>
</a>
apply. The document
has one of the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ db: <database>, collection: <collection> }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">db</span>: <<!-- -->database<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">collection</span>: <<!-- -->collection<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
or
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ cluster : true }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->cluster <!-- -->: <span class="lg-highlight-literal">true</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-document">
Resource Document on Self-Managed Deployments
</a>
for more details.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
admin.system.roles.privileges[n].actions
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-admin.system.roles.privileges-n-.actions" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-admin.system.roles.privileges-n-.actions">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
An array of actions permitted on the resource. For a list of
actions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
Privilege Actions for Self-Managed Deployments
<!-- -->
.
</a>
</p>
</dd>
</dl>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
admin.system.roles.roles
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-admin.system.roles.roles" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-admin.system.roles.roles">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-admin.system.roles.roles" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
roles
</code>
</span>
</a>
array contains role documents
that specify the roles from which this role
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-inheritance">
inherits
</a>
privileges.
</p>
<p class="leafygreen-ui-1kp3ins">
A role document has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ role: \"<role>\", db: \"<database>\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"<role name>"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"<database>"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
A role document has the following fields:
</p>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
admin.system.roles.roles[n].role
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-admin.system.roles.roles-n-.role" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-admin.system.roles.roles-n-.role">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The name of the role. A role can be a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#std-label-built-in-roles">
built-in role
</a>
provided by MongoDB or a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-user-defined-roles/#std-label-user-defined-roles">
user-defined
role
<!-- -->
.
</a>
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
admin.system.roles.roles[n].db
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-admin.system.roles.roles-n-.db" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-admin.system.roles.roles-n-.db">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The name of the database where the role is defined.
</p>
</dd>
</dl>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Consider the following sample documents found in
<code class="leafygreen-ui-1l06pbn">
system.roles
</code>
collection of the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
A User-Defined Role Specifies Privileges
<a class="headerlink leafygreen-ui-1kepx47" href="#a-user-defined-role-specifies-privileges" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="a-user-defined-role-specifies-privileges">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following is a sample document for a user-defined role
<code class="leafygreen-ui-1l06pbn">
appUser
</code>
defined for the
<code class="leafygreen-ui-1l06pbn">
myApp
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: \"myApp.appUser\",\n role: \"appUser\",\n db: \"myApp\",\n privileges: [\n { resource: { db: \"myApp\" , collection: \"\" },\n actions: [ \"find\", \"createCollection\", \"dbStats\", \"collStats\" ] },\n { resource: { db: \"myApp\", collection: \"logs\" },\n actions: [ \"insert\" ] },\n { resource: { db: \"myApp\", collection: \"data\" },\n actions: [ \"insert\", \"update\", \"remove\", \"compact\" ] },\n { resource: { db: \"myApp\", collection: \"system.js\" },\n actions: [ \"find\" ] },\n ],\n roles: []\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"myApp.appUser"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"appUser"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"myApp"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"myApp"</span> <!-- -->, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">""</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span>, <span class="lg-highlight-string">"createCollection"</span>, <span class="lg-highlight-string">"dbStats"</span>, <span class="lg-highlight-string">"collStats"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"myApp"</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">"logs"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"insert"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"myApp"</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">"data"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"insert"</span>, <span class="lg-highlight-string">"update"</span>, <span class="lg-highlight-string">"remove"</span>, <span class="lg-highlight-string">"compact"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"myApp"</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">"system.js"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[<!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
privileges
</code>
array lists the five privileges that the
<code class="leafygreen-ui-1l06pbn">
appUser
</code>
role specifies:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The first privilege permits its actions (
<code class="leafygreen-ui-1l06pbn">
"find"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"createCollection"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"dbStats"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"collStats"
</code>
) on all the
collections in the
<code class="leafygreen-ui-1l06pbn">
myApp
</code>
database
<em>
excluding
</em>
its system
collections. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-specific-db">
Specify a Database as Resource
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The next two privileges permits
<em>
additional
</em>
actions on specific
collections,
<code class="leafygreen-ui-1l06pbn">
logs
</code>
and
<code class="leafygreen-ui-1l06pbn">
data
</code>
, in the
<code class="leafygreen-ui-1l06pbn">
myApp
</code>
database. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-specific-db-collection">
Specify a Collection of a Database as Resource
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The last privilege permits actions on one
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-collections/">
system
collections
</a>
in the
<code class="leafygreen-ui-1l06pbn">
myApp
</code>
database. While the first privilege gives database-wide permission
for the
<code class="leafygreen-ui-1l06pbn">
find
</code>
action, the action does not apply to
<code class="leafygreen-ui-1l06pbn">
myApp
</code>
's
system collections. To give access to a system collection, a
privilege must explicitly specify the collection. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/">
Resource Document on Self-Managed Deployments
<!-- -->
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
As indicated by the empty
<code class="leafygreen-ui-1l06pbn">
roles
</code>
array,
<code class="leafygreen-ui-1l06pbn">
appUser
</code>
inherits no
additional privileges from other roles.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
User-Defined Role Inherits from Other Roles
<a class="headerlink leafygreen-ui-1kepx47" href="#user-defined-role-inherits-from-other-roles" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="user-defined-role-inherits-from-other-roles">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following is a sample document for a user-defined role
<code class="leafygreen-ui-1l06pbn">
appAdmin
</code>
defined for the
<code class="leafygreen-ui-1l06pbn">
myApp
</code>
database: The document shows that the
<code class="leafygreen-ui-1l06pbn">
appAdmin
</code>
role specifies privileges as well as inherits privileges
from other roles:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: \"myApp.appAdmin\",\n role: \"appAdmin\",\n db: \"myApp\",\n privileges: [\n {\n resource: { db: \"myApp\", collection: \"\" },\n actions: [ \"insert\", \"dbStats\", \"collStats\", \"compact\" ]\n }\n ],\n roles: [\n { role: \"appUser\", db: \"myApp\" }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"myApp.appAdmin"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"appAdmin"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"myApp"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"myApp"</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">""</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"insert"</span>, <span class="lg-highlight-string">"dbStats"</span>, <span class="lg-highlight-string">"collStats"</span>, <span class="lg-highlight-string">"compact"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"appUser"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"myApp"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
privileges
</code>
array lists the privileges that the
<code class="leafygreen-ui-1l06pbn">
appAdmin
</code>
role specifies. This role has a single privilege that permits its
actions (
<code class="leafygreen-ui-1l06pbn">
"insert"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"dbStats"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"collStats"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"compact"
</code>
)
on all the collections in the
<code class="leafygreen-ui-1l06pbn">
myApp
</code>
database
<em>
excluding
</em>
its system
collections. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-specific-db">
Specify a Database as Resource
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
roles
</code>
array lists the roles, identified by the role names and
databases, from which the role
<code class="leafygreen-ui-1l06pbn">
appAdmin
</code>
inherits privileges.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/update/mul/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$mul
<a class="headerlink leafygreen-ui-1kepx47" href="#-mul" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-mul">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="update">
<dt>
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-update-up.-mul" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-update-up.-mul">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Multiply the value of a field by a number. To specify a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-mul" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
</span>
</a>
expression, use the following prototype:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $mul: { <field1>: <number1>, ... } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$mul</span>: <!-- -->{ <<!-- -->field1<!-- -->><!-- -->: <<!-- -->number1<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> } }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The field to update must contain a numeric value.
</p>
<p class="leafygreen-ui-1kp3ins">
To specify a
<code class="leafygreen-ui-1l06pbn">
<field>
</code>
in an embedded document or in an array, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-document-dot-notation">
dot notation
<!-- -->
.
</a>
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
no longer raises an
error when you use an update operator like
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-mul" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
</span>
</a>
with an empty operand expression (
<code class="leafygreen-ui-1l06pbn">
{ }
</code>
). An empty update results
in no changes and no
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
entry is created (meaning that the
operation is a no-op).
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, update operators process document fields with
string-based names in lexicographic order. Fields with numeric names are
processed in numeric order. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/#std-label-update-operators-processing-order">
Update Operators Behavior
</a>
for details.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Missing Field
<a class="headerlink leafygreen-ui-1kepx47" href="#missing-field" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="missing-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the field does not exist in a document,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-mul" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
</span>
</a>
creates the
field and sets the value to zero of the same numeric type as the
multiplier.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Atomic
<a class="headerlink leafygreen-ui-1kepx47" href="#atomic" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="atomic">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-mul" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
</span>
</a>
is an atomic operation within a single document.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-multiplication-type-conversion">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Mixed Type
<a class="headerlink leafygreen-ui-1kepx47" href="#mixed-type" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mixed-type">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Multiplication with values of mixed numeric types (32-bit integer,
64-bit integer, float) may result in conversion of numeric type. For
multiplication with values of mixed numeric types, the following type
conversion rules apply:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
32-bit Integer
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
64-bit Integer
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Float
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
32-bit Integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
32-bit or 64-bit Integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
64-bit Integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Float
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
64-bit Integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
64-bit Integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
64-bit Integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Float
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
Float
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Float
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Float
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Float
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the product of two 32-bit integers exceeds the maximum value
for a 32-bit integer, the result is a 64-bit integer.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Integer operations of any type that exceed the maximum value for a
64-bit integer produce an error.
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Multiply the Value of a Field
<a class="headerlink leafygreen-ui-1kepx47" href="#multiply-the-value-of-a-field" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="multiply-the-value-of-a-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create the
<code class="leafygreen-ui-1l06pbn">
products
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.insertOne(\n { \"_id\" : 1, \"item\" : \"Hats\", \"price\" : Decimal128(\"10.99\"), \"quantity\" : 25 }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Hats"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>(<span class="lg-highlight-string">"10.99"</span>)<!-- -->, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">25</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the following operation,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateOne()
</code>
</a>
updates
the document. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-mul" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
</span>
</a>
operator multiplies the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field by
<code class="leafygreen-ui-1l06pbn">
1.25
</code>
and the
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
field by
<code class="leafygreen-ui-1l06pbn">
2
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.updateOne(\n { _id: 1 },\n { $mul:\n {\n price: Decimal128( \"1.25\" ),\n quantity: 2\n }\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$mul</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"1.25"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the updated document:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
price
</code>
is the original value, 10.99, multiplied by 1.25
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
is the original value, 25, multiplied by 2
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: 1, item: 'Hats', price: Decimal128(\"13.7375\"), quantity: 50 }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">'Hats'</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>(<span class="lg-highlight-string">"13.7375"</span>)<!-- -->, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">50</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Apply
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
Operator to a Non-existing Field
<a class="headerlink leafygreen-ui-1kepx47" href="#apply--mul-operator-to-a-non-existing-field" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="apply--mul-operator-to-a-non-existing-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Add the following document to the
<code class="leafygreen-ui-1l06pbn">
products
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.insertOne( { _id: 2, item: \"Unknown\" } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Unknown"</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the following operation,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateOne()
</code>
</a>
attempts to
apply the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-mul" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
</span>
</a>
operator to a field that is not in the document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.updateOne(\n { _id: 2 },\n { $mul: { price: Decimal128(\"100\") } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$mul</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>(<span class="lg-highlight-string">"100"</span>) } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateOne()
</code>
</a>
operation
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
inserts the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
sets Decimal128("0")
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 2, \"item\" : \"Unknown\", \"price\" : NumberLong(0) }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Unknown"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>) }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
price
</code>
field has the same type, Decimal128, as the multiplier.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Multiply Mixed Numeric Types
<a class="headerlink leafygreen-ui-1kepx47" href="#multiply-mixed-numeric-types" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="multiply-mixed-numeric-types">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Add the following document to the
<code class="leafygreen-ui-1l06pbn">
products
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.insertOne( { _id: 3, item: \"Scarf\", price: Decimal128(\"10\") } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"Scarf"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>(<span class="lg-highlight-string">"10"</span>) } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the following operation,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateOne()
</code>
</a>
uses
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-mul" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$mul
</code>
</span>
</a>
operator to multiply the value in the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
Decimal128(10)
</span>
</a>
by
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-int" target="_self">
<span>
Int32(5)
<!-- -->
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.updateOne(\n { _id: 3 },\n { $mul: { price: Int32(5) } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$mul</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-title lg-highlight-class">Int32</span>(<span class="lg-highlight-number">5</span>) } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation results in the following document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: 3, item: 'Scarf', price: Decimal128(\"50\") }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">'Scarf'</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>(<span class="lg-highlight-string">"50"</span>) }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The value in the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field is of type
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
Decimal128
</span>
</a>
. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-multiplication-type-conversion" target="_self">
<span>
Multiplication Type Conversion Rules
</span>
</a>
for details.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateMany/#mongodb-method-db.collection.updateMany">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
<code class="leafygreen-ui-1l06pbn">
db.collection.findAndModify()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/update/positional/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$ (update)
<a class="headerlink leafygreen-ui-1kepx47" href="#---update-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="---update-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="update">
<dt>
<code class="leafygreen-ui-1l06pbn">
$
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-update-up.-" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-update-up.-">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator identifies an element in
an array to update without explicitly specifying the position of the
element in the array.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Disambiguation
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To project, or return, an array element from a read operation,
see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/positional/#mongodb-projection-proj.-">
<code class="leafygreen-ui-1l06pbn">
$
</code>
</a>
projection operator instead.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To update all elements in an array, see the all positional
operator
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/positional-all/#mongodb-update-up.---">
<code class="leafygreen-ui-1l06pbn">
$[]
</code>
</a>
instead.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To update all elements that match an array filter condition or
conditions, see the filtered positional operator instead
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/positional-filtered/#mongodb-update-up.---identifier--">
<code class="leafygreen-ui-1l06pbn">
$[<identifier>]
</code>
.
</a>
</p>
</li>
</ul>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can use
<!-- -->
the positional
<code class="leafygreen-ui-1l06pbn">
$
</code>
operator
<!-- -->
for deployments hosted in the following
environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator has the form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"<array>.$\" : value }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"<array>.$"</span> <!-- -->: <!-- -->value }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
When used with update operations, e.g.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateOne()
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
<code class="leafygreen-ui-1l06pbn">
db.collection.findAndModify()
</code>
,
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator acts as a placeholder for
the
<strong>
first
</strong>
element that matches the
<code class="leafygreen-ui-1l06pbn">
query document
</code>
, and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<code class="leafygreen-ui-1l06pbn">
array
</code>
field
<strong>
must
</strong>
appear as part of the
<code class="leafygreen-ui-1l06pbn">
query
document
</code>
.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.updateOne(\n { <array>: value ... },\n { <update>: { \"<array>.$\" : value } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <<!-- -->array<!-- -->><!-- -->: <!-- -->value <!-- -->...<!-- --> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- --><update operator>: { "<array>.$" : value } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, update operators process document fields with
string-based names in lexicographic order. Fields with numeric names are
processed in numeric order. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/#std-label-update-operators-processing-order">
Update Operators Behavior
</a>
for details.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
upsert
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#upsert" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="upsert">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Do not use the positional operator
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-upsert">
upsert
</a>
operations because inserts will use the
<code class="leafygreen-ui-1l06pbn">
$
</code>
as
a field name in the inserted document.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Nested Arrays
<a class="headerlink leafygreen-ui-1kepx47" href="#nested-arrays" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="nested-arrays">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator cannot be used for queries which
traverse more than one array, such as queries that traverse arrays
nested within other arrays, because the replacement for the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
placeholder is a single value
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Unsets
<a class="headerlink leafygreen-ui-1kepx47" href="#unsets" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="unsets">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When used with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/unset/#mongodb-update-up.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
operator, the positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator does not remove the matching element
from the array but rather sets it to
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Negations
<a class="headerlink leafygreen-ui-1kepx47" href="#negations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="negations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the query matches the array using a negation operator, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/ne/#mongodb-query-op.-ne">
<code class="leafygreen-ui-1l06pbn">
$ne
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/not/#mongodb-query-op.-not">
<code class="leafygreen-ui-1l06pbn">
$not
</code>
</a>
, or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/nin/#mongodb-query-op.-nin">
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</a>
, then you cannot use the
positional operator to update values from this array.
</p>
<p class="leafygreen-ui-1kp3ins">
However, if the negated portion of the query is inside of an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/elemMatch/#mongodb-query-op.-elemMatch">
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</a>
expression, then you
<em>
can
</em>
use the positional
operator to update this field.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Multiple Array Matches
<a class="headerlink leafygreen-ui-1kepx47" href="#multiple-array-matches" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="multiple-array-matches">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
update operator behaves ambiguously when filtering
on multiple array fields.
</p>
<p class="leafygreen-ui-1kp3ins">
When the server executes an update method, it first runs a query to determine
which documents you want to update. If the update filters documents on multiple
array fields, the subsequent call to the positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
update operator
doesn't always update the required position in the array.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-multiple-array-match" target="_self">
<span>
example
<!-- -->
.
</span>
</a>
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update Values in an Array
<a class="headerlink leafygreen-ui-1kepx47" href="#update-values-in-an-array" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="update-values-in-an-array">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
students
</code>
with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.insertMany( [\n { \"_id\" : 1, \"grades\" : [ 85, 80, 80 ] },\n { \"_id\" : 2, \"grades\" : [ 88, 90, 92 ] },\n { \"_id\" : 3, \"grades\" : [ 85, 100, 90 ] }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">students</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">85</span>, <span class="lg-highlight-number">80</span>, <span class="lg-highlight-number">80</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">88</span>, <span class="lg-highlight-number">90</span>, <span class="lg-highlight-number">92</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">85</span>, <span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">90</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To update the first element whose value is
<code class="leafygreen-ui-1l06pbn">
80
</code>
to
<code class="leafygreen-ui-1l06pbn">
82
</code>
in the in
the
<code class="leafygreen-ui-1l06pbn">
grades
</code>
array, use the positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator if you do
not know the position of the element in the array:
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
You must include the array field as part of the
<code class="leafygreen-ui-1l06pbn">
query
</code>
document.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.updateOne(\n { _id: 1, grades: 80 },\n { $set: { \"grades.$\" : 82 } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">students</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">grades</span>: <span class="lg-highlight-number">80</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"grades.$"</span> <!-- -->: <span class="lg-highlight-number">82</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator acts as a placeholder for the
<strong>
first match
</strong>
of the update
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-documents/#std-label-read-operations-query-document">
query document
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
After the operation, the
<code class="leafygreen-ui-1l06pbn">
students
</code>
collection contains the following
documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"grades\" : [ 85, 82, 80 ] }\n{ \"_id\" : 2, \"grades\" : [ 88, 90, 92 ] }\n{ \"_id\" : 3, \"grades\" : [ 85, 100, 90 ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">85</span>, <span class="lg-highlight-number">82</span>, <span class="lg-highlight-number">80</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">88</span>, <span class="lg-highlight-number">90</span>, <span class="lg-highlight-number">92</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">85</span>, <span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">90</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update Documents in an Array
<a class="headerlink leafygreen-ui-1kepx47" href="#update-documents-in-an-array" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="update-documents-in-an-array">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator facilitates updates to arrays
that contain embedded documents. Use the positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator to access the fields in the embedded documents with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-document-dot-notation">
dot notation
</a>
on the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.updateOne(\n { <query> },\n { <update>: { \"array.$.field\" : value } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- --><query selector> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { <update operator>: { "array.$.field" : value } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Consider the following document in the
<code class="leafygreen-ui-1l06pbn">
students
</code>
collection whose
<code class="leafygreen-ui-1l06pbn">
grades
</code>
element value is an array of embedded documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 4,\n grades: [\n { grade: 80, mean: 75, std: 8 },\n { grade: 85, mean: 90, std: 5 },\n { grade: 85, mean: 85, std: 8 }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">grades</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">80</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">8</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">5</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">8</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Use the positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator to update the
<code class="leafygreen-ui-1l06pbn">
std
</code>
field of
the first array element that matches the
<code class="leafygreen-ui-1l06pbn">
grade
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
85
</code>
condition:
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
You must include the array field as part of the
<code class="leafygreen-ui-1l06pbn">
query
</code>
document.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.updateOne(\n { _id: 4, \"grades.grade\": 85 },\n { $set: { \"grades.$.std\" : 6 } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">students</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"grades.grade"</span>: <span class="lg-highlight-number">85</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"grades.$.std"</span> <!-- -->: <span class="lg-highlight-number">6</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
After the operation, the document has the following updated values:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 4,\n \"grades\" : [\n { \"grade\" : 80, \"mean\" : 75, \"std\" : 8 },\n { \"grade\" : 85, \"mean\" : 90, \"std\" : 6 },\n { \"grade\" : 85, \"mean\" : 85, \"std\" : 8 }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">80</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">8</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">8</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-multiple-point-matches">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update Embedded Documents Using Multiple Field Matches
<a class="headerlink leafygreen-ui-1kepx47" href="#update-embedded-documents-using-multiple-field-matches" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="update-embedded-documents-using-multiple-field-matches">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator can update the first array element that matches
multiple query criteria specified with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/elemMatch/#mongodb-query-op.-elemMatch">
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</a>
operator.
</p>
<p class="leafygreen-ui-1kp3ins">
Consider the following document in the
<code class="leafygreen-ui-1l06pbn">
students
</code>
collection whose
<code class="leafygreen-ui-1l06pbn">
grades
</code>
field value is an array of embedded documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 5,\n grades: [\n { grade: 80, mean: 75, std: 8 },\n { grade: 85, mean: 90, std: 5 },\n { grade: 90, mean: 85, std: 3 }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">grades</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">80</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">8</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">5</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">3</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the example below, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
operator updates the value of the
<code class="leafygreen-ui-1l06pbn">
std
</code>
field in the first embedded document that has
<code class="leafygreen-ui-1l06pbn">
grade
</code>
field with
a value less than or equal to
<code class="leafygreen-ui-1l06pbn">
90
</code>
and a
<code class="leafygreen-ui-1l06pbn">
mean
</code>
field with a value
greater than
<code class="leafygreen-ui-1l06pbn">
80
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.updateOne(\n {\n _id: 5,\n grades: { $elemMatch: { grade: { $lte: 90 }, mean: { $gt: 80 } } }\n },\n { $set: { \"grades.$.std\" : 6 } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">students</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">grades</span>: <!-- -->{ <span class="lg-highlight-attr">$elemMatch</span>: <!-- -->{ <span class="lg-highlight-attr">grade</span>: <!-- -->{ <span class="lg-highlight-attr">$lte</span>: <span class="lg-highlight-number">90</span> }<!-- -->, <span class="lg-highlight-attr">mean</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">80</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"grades.$.std"</span> <!-- -->: <span class="lg-highlight-number">6</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
This operation updates the first embedded document that matches the
criteria, namely the second embedded document in the array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 5,\n grades: [\n { grade: 80, mean: 75, std: 8 },\n { grade: 85, mean: 90, std: 6 },\n { grade: 90, mean: 85, std: 3 }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">grades</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">80</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">8</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">grade</span>: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-attr">mean</span>: <span class="lg-highlight-number">85</span>, <span class="lg-highlight-attr">std</span>: <span class="lg-highlight-number">3</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-multiple-array-match">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update with Multiple Array Matches
<a class="headerlink leafygreen-ui-1kepx47" href="#update-with-multiple-array-matches" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="update-with-multiple-array-matches">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
update operator behaves ambiguously when the
query has multiple array fields to filter documents in the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
Consider a document in the
<code class="leafygreen-ui-1l06pbn">
students_deans_list
</code>
collection, which holds
arrays of student information:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students_deans_list.insertMany( [\n {\n _id: 8,\n activity_ids: [ 1, 2 ],\n grades: [ 90, 95 ],\n deans_list: [ 2021, 2020 ]\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">students_deans_list</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">8</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">activity_ids</span>: <!-- -->[ <span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">2</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">grades</span>: <!-- -->[ <span class="lg-highlight-number">90</span>, <span class="lg-highlight-number">95</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">deans_list</span>: <!-- -->[ <span class="lg-highlight-number">2021</span>, <span class="lg-highlight-number">2020</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the following example, the user attempts to modify the
<code class="leafygreen-ui-1l06pbn">
deans_list
</code>
field,
filtering documents using the
<code class="leafygreen-ui-1l06pbn">
activity_ids
</code>
,
<code class="leafygreen-ui-1l06pbn">
deans_list
</code>
, and
<code class="leafygreen-ui-1l06pbn">
grades
</code>
fields, and updating the 2021 value in the
<code class="leafygreen-ui-1l06pbn">
deans_list
</code>
field to 2022:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students_deans_list.updateOne(\n { activity_ids: 1, grades: 95, deans_list: 2021 },\n { $set: { \"deans_list.$\": 2022 } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">students_deans_list</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">activity_ids</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">grades</span>: <span class="lg-highlight-number">95</span>, <span class="lg-highlight-attr">deans_list</span>: <span class="lg-highlight-number">2021</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"deans_list.$"</span>: <span class="lg-highlight-number">2022</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
When the server executes the
<code class="leafygreen-ui-1l06pbn">
updateOne
</code>
method above, it filters
the available documents using values in the supplied array fields.
Although the
<code class="leafygreen-ui-1l06pbn">
deans_list
</code>
field is used in the filter, it is not the field
used by the positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
update operator to determine which position
in the array to update:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students_deans_list.find( { _id: 8 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">students_deans_list</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">8</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 8,\n activity_ids: [ 1, 2 ],\n grades: [ 90, 95 ],\n deans_list: [ 2021, 2022 ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">8</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">activity_ids</span>: <!-- -->[ <span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">2</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">grades</span>: <!-- -->[ <span class="lg-highlight-number">90</span>, <span class="lg-highlight-number">95</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">deans_list</span>: <!-- -->[ <span class="lg-highlight-number">2021</span>, <span class="lg-highlight-number">2022</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
updateOne
</code>
method matched the
<code class="leafygreen-ui-1l06pbn">
deans_list
</code>
field on 2021, but the
positional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-update-up.-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$
</code>
</span>
</a>
update operator instead changed the 2020 value to 2022.
</p>
<p class="leafygreen-ui-1kp3ins">
To avoid unexpected results when matching on multiple arrays, instead
use the filtered positional operator
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/positional-filtered/#mongodb-update-up.---identifier--">
<code class="leafygreen-ui-1l06pbn">
$[<identifier>]
</code>
.
</a>
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateMany/#mongodb-method-db.collection.updateMany">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
<code class="leafygreen-ui-1l06pbn">
db.collection.findAndModify()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/elemMatch/#mongodb-query-op.-elemMatch">
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/backups/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Backup Methods for a Self-Managed Deployment
<a class="headerlink leafygreen-ui-1kepx47" href="#backup-methods-for-a-self-managed-deployment" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="backup-methods-for-a-self-managed-deployment">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#back-up-with-mms-or-ops-manager" target="_self">
<span>
Back Up with
<!-- -->
MongoDB Cloud Manager
<!-- -->
or Ops Manager
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#back-up-by-copying-underlying-data-files" target="_self">
<span>
Back Up by Copying Underlying Data Files
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#back-up-with-mongodump" target="_self">
<span>
Back Up with
<code class="css-1kekw28 e1wawog0">
mongodump
</code>
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
When deploying MongoDB in production, you should have a
strategy for capturing and restoring backups in the case of data loss
events.
</p>
<p class="leafygreen-ui-1kp3ins">
This page covers
<!-- -->
backup methods
<!-- -->
for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-self-managed">
self-managed
</a>
deployments.
</p>
<p class="leafygreen-ui-1kp3ins">
To learn more about
<!-- -->
Backup Methods
<!-- -->
for deployments hosted in MongoDB
Atlas, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/backup-restore-cluster/" target="_self">
<span>
Back Up, Restore, and Archive Data
</span>
</a>
.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Back Up with
<!-- -->
MongoDB Cloud Manager
<!-- -->
or Ops Manager
<a class="headerlink leafygreen-ui-1kepx47" href="#back-up-with-mms-or-ops-manager" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="back-up-with-mms-or-ops-manager">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB Cloud Manager
<!-- -->
is a hosted back up, monitoring, and automation service for
MongoDB.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cloud.mongodb.com/?tck=docs_server" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
supports backing up and restoring MongoDB
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica sets
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded clusters
</a>
from a graphical user interface.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-backup-with-mms">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
MongoDB Cloud Manager
<a class="headerlink leafygreen-ui-1kepx47" href="#mms" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mms">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cloud.mongodb.com/?tck=docs_server" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
supports the backing up and restoring of MongoDB
deployments.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB Cloud Manager
<!-- -->
continually backs up MongoDB
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica sets
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded clusters
</a>
by reading the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
data from your MongoDB deployment.
<!-- -->
MongoDB Cloud Manager
<!-- -->
creates snapshots
of your data at set intervals, and can also offer point-in-time recovery
of MongoDB replica sets and sharded clusters.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Sharded cluster snapshots are difficult to achieve with other MongoDB
backup methods.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To get started with
<!-- -->
MongoDB Cloud Manager
<!-- -->
Backup, sign up for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cloud.mongodb.com/?tck=docs_server" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
. For
documentation on
<!-- -->
MongoDB Cloud Manager
<!-- -->
, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/cloud-manager/" target="_self">
<span>
MongoDB Cloud Manager documentation
</span>
</a>
.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-backup-with-mms-onprem">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Ops Manager
<a class="headerlink leafygreen-ui-1kepx47" href="#ops-manager" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="ops-manager">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
With Ops Manager, MongoDB subscribers can install and run the same core
software that powers
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-backup-with-mms" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
on their own infrastructure.
Ops Manager is an on-premise solution that has similar functionality to
<!-- -->
MongoDB Cloud Manager
<!-- -->
and is available with Enterprise Advanced subscriptions.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information about Ops Manager, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/mongodb-enterprise-advanced?tck=docs_server" target="_self">
<span>
MongoDB Enterprise
Advanced
</span>
</a>
page
and the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/ops-manager/current/" target="_self">
<span>
Ops Manager Manual
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-backup-with-file-copies">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Back Up by Copying Underlying Data Files
<a class="headerlink leafygreen-ui-1kepx47" href="#back-up-by-copying-underlying-data-files" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="back-up-by-copying-underlying-data-files">
</div>
</a>
</h2>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Considerations for Encrypted Storage Engines using AES256-GCM
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
For
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-encryption-at-rest/#std-label-encrypted-storage-engine">
encrypted storage engines
</a>
that
use
<code class="leafygreen-ui-1l06pbn">
AES256-GCM
</code>
encryption mode,
<code class="leafygreen-ui-1l06pbn">
AES256-GCM
</code>
requires that every
process use a unique counter block value with the key.
</p>
<p class="leafygreen-ui-1kp3ins">
For
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-encryption-at-rest/#std-label-encrypted-storage-engine">
encrypted storage engine
</a>
configured with
<code class="leafygreen-ui-1l06pbn">
AES256-GCM
</code>
cipher:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
Restoring from Hot Backup
</dt>
<dd class="css-1ufxuu7">
Starting in 4.2, if you restore from files taken via "hot"
backup (i.e. the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
is running), MongoDB
can detect "dirty" keys on startup and automatically rollover
the database key to avoid IV (Initialization Vector) reuse.
</dd>
</dl>
</li>
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
Restoring from Cold Backup
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
However, if you restore from files taken via "cold" backup
(i.e. the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
is not running), MongoDB
cannot detect "dirty" keys on startup, and reuse of IV voids
confidentiality and integrity guarantees.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in 4.2, to avoid the reuse of the keys after
restoring from a cold filesystem snapshot, MongoDB adds a new
command-line option
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--eseDatabaseKeyRollover">
<code class="leafygreen-ui-1l06pbn">
--eseDatabaseKeyRollover
</code>
</a>
. When started with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--eseDatabaseKeyRollover">
<code class="leafygreen-ui-1l06pbn">
--eseDatabaseKeyRollover
</code>
</a>
option, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance rolls over the database keys configured with
<code class="leafygreen-ui-1l06pbn">
AES256-GCM
</code>
cipher and exits.
</p>
</dd>
</dl>
</li>
</ul>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In general, if using filesystem based backups for MongoDB Enterprise, use
the "hot" backup feature, if possible.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Back Up with Filesystem Snapshots
<a class="headerlink leafygreen-ui-1kepx47" href="#back-up-with-filesystem-snapshots" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="back-up-with-filesystem-snapshots">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can create a backup of a MongoDB deployment by making a copy of MongoDB's
underlying data files.
</p>
<p class="leafygreen-ui-1kp3ins">
If the volume where MongoDB stores its data files supports point-in-time
snapshots, you can use these snapshots to create backups of a MongoDB
system at an exact moment in time.
File system snapshots are an operating system volume manager feature,
and are not specific to MongoDB. With file system snapshots, the operating
system takes a snapshot of the volume to use as a baseline for data backup.
The mechanics of snapshots depend on
the underlying storage system. For example, on Linux, the Logical Volume
Manager (LVM) can create snapshots. Similarly, Amazon's EBS storage
system for EC2 supports snapshots.
</p>
<p class="leafygreen-ui-1kp3ins">
To get a correct snapshot of a running
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
process, you
must have journaling enabled and the journal must reside on the same
logical volume as the other MongoDB data files. Without journaling
enabled, there is no guarantee that the snapshot will be consistent or
valid.
</p>
<p class="leafygreen-ui-1kp3ins">
To get a consistent snapshot of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded cluster
</a>
, you must
disable the balancer and capture a snapshot from every shard as well as a
config server at approximately the same moment in time.
To backup sharded clusters, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/backup-sharded-cluster-with-database-dumps/#std-label-backup-sharded-dumps">
Back Up a Self-Managed Sharded Cluster with a Database Dump
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/backup-with-filesystem-snapshots/">
Back Up and Restore a Self-Managed Deployment with Filesystem Snapshots
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/backup-sharded-cluster-with-filesystem-snapshots/">
Back Up a Self-Managed Sharded Cluster with File System Snapshots
</a>
for
complete instructions on using LVM to create snapshots.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Back Up with
<code class="leafygreen-ui-1l06pbn">
cp
</code>
or
<code class="leafygreen-ui-1l06pbn">
rsync
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#back-up-with-cp-or-rsync" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="back-up-with-cp-or-rsync">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If your storage system does not support snapshots, you can copy the
files directly using
<code class="leafygreen-ui-1l06pbn">
cp
</code>
,
<code class="leafygreen-ui-1l06pbn">
rsync
</code>
, or a similar tool. Since
copying multiple files is not an atomic operation, you must stop all
writes to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
before copying the files. Otherwise, you will
copy the files in an invalid state.
</p>
<p class="leafygreen-ui-1kp3ins">
Backups produced by copying the underlying data do not support point
in time recovery for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica sets
</a>
and are difficult to manage for
larger sharded clusters. Additionally, these backups are larger
because they include the indexes and duplicate underlying storage
padding and fragmentation.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
, by contrast, creates
smaller backups.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-backup-with-mongodump">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Back Up with
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#back-up-with-mongodump" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="back-up-with-mongodump">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
reads data from a MongoDB database and
creates high fidelity BSON files which the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongorestore/#mongodb-binary-bin.mongorestore" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongorestore
</code>
</span>
</a>
tool can use to populate a MongoDB database.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongorestore/#mongodb-binary-bin.mongorestore" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongorestore
</code>
</span>
</a>
are simple and
efficient tools for backing up and restoring small
MongoDB deployments, but are not ideal for capturing backups of larger
systems.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongorestore/#mongodb-binary-bin.mongorestore" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongorestore
</code>
</span>
</a>
operate against a
running
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
process, and can manipulate the underlying
data files directly. By default,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
does not
capture the contents of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/local-database/#std-label-replica-set-local-database">
local database
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
only captures the documents in the database. The
resulting backup is space efficient, but
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongorestore/#mongodb-binary-bin.mongorestore" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongorestore
</code>
</span>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
must rebuild the indexes after restoring data.
</p>
<p class="leafygreen-ui-1kp3ins">
When connected to a MongoDB instance,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
can
adversely affect
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
performance. If your data is larger
than system memory, the queries will push the working set out of
memory, causing page faults.
</p>
<p class="leafygreen-ui-1kp3ins">
Applications can continue to modify data while
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
captures the output. For replica sets,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
provides
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#std-option-mongodump.--oplog" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--oplog
</code>
</span>
</a>
option to include in its
output
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
entries that occur during the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
operation. This allows the corresponding
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongorestore/#mongodb-binary-bin.mongorestore" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongorestore
</code>
</span>
</a>
operation to replay the captured oplog. To restore a backup created
with
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#std-option-mongodump.--oplog" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--oplog
</code>
</span>
</a>
, use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongorestore/#mongodb-binary-bin.mongorestore" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongorestore
</code>
</span>
</a>
with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongorestore/#std-option-mongorestore.--oplogReplay" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--oplogReplay
</code>
</span>
</a>
option.
</p>
<p class="leafygreen-ui-1kp3ins">
However, for replica sets, consider
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-backup-with-mms" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-backup-with-mms-onprem" target="_self">
<span>
Ops Manager
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To backup sharded clusters, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/backup-sharded-cluster-with-database-dumps/#std-label-backup-sharded-dumps">
Back Up a Self-Managed Sharded Cluster with a Database Dump
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
To use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongodump/#std-program-mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongorestore/#std-program-mongorestore" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongorestore
</code>
</span>
</a>
as a backup
strategy for sharded clusters, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/backup-sharded-cluster-with-database-dumps/#std-label-backup-sharded-dumps">
Back Up a Self-Managed Sharded Cluster with a Database Dump
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Sharded clusters can also use one of the following coordinated backup
and restore processes, which maintain the atomicity guarantees of
transactions across shards:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/atlas/database?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/cloud/cloud-manager?tck=docs_server" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/ops-manager?tck=docs_server" target="_self">
<span>
MongoDB Ops Manager
</span>
</a>
</p>
</li>
</ul>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/backup-and-restore-tools/">
Back Up and Restore a Self-Managed Deployment with MongoDB Tools
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/backup-sharded-cluster-with-database-dumps/">
Back Up a Self-Managed Sharded Cluster with a Database Dump
</a>
for more information.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/cosh/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$cosh (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-cosh--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-cosh--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-cosh" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-cosh">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the hyperbolic cosine of a value that is measured in radians.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $cosh: <expression> }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$cosh</span>: <<!-- -->expression<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
takes any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a number, measured in
radians. If the expression returns a value in degrees, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/degreesToRadians/#mongodb-expression-exp.-degreesToRadians">
<code class="leafygreen-ui-1l06pbn">
$degreesToRadians
</code>
</a>
operator to convert the value to
radians.
</p>
<p class="leafygreen-ui-1kp3ins">
By default
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
returns values as a
<code class="leafygreen-ui-1l06pbn">
double
</code>
.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
can also return values as a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimal
</span>
</a>
if the
<code class="leafygreen-ui-1l06pbn">
<expression>
</code>
resolves to a 128-bit
decimal value.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
</dd>
</dl>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
null
</code>
,
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
, and
<code class="leafygreen-ui-1l06pbn">
+/- Infinity
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#null--nan--and-----infinity" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="null--nan--and-----infinity">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the input argument resolves to a value of
<code class="leafygreen-ui-1l06pbn">
null
</code>
or refers to a
field that is missing,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
. If the
argument resolves to
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
. If
the argument resolves to negative or positive
<code class="leafygreen-ui-1l06pbn">
Infinity
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
returns positive
<code class="leafygreen-ui-1l06pbn">
Infinity
</code>
.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:50%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Results
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $cosh: NaN }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $cosh: null }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
null
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $cosh: -Infinity }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
Infinity
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $cosh: Infinity }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
Infinity
</code>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-8">
<div aria-label="Tabs to describe usage of degrees/radians" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_qhbrjqwtg" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_nk6ejjvvt" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Hyperbolic Cosine in Degrees
</div>
</button>
<button aria-controls="_thl9lqxuk" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_fq1ukp70z" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Hyperbolic Cosine in Radians
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_v6feisbw5" class="" id="_dex9tlmir" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
trigonometry
</code>
collection contains a document
that stores an
<code class="leafygreen-ui-1l06pbn">
angle
</code>
value measured in degrees:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.insertOne(\n {\n \"_id\" : ObjectId( \"5c50782193f833234ba90d85\" ),\n \"angle\" : NumberDecimal( \"53.1301023541559787031443874490659\" )\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">trigonometry</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>( <span class="lg-highlight-string">"5c50782193f833234ba90d85"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>( <span class="lg-highlight-string">"53.1301023541559787031443874490659"</span> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
expression to calculate the hyperbolic
cosine of
<code class="leafygreen-ui-1l06pbn">
angle
</code>
and adds it to the input document using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
pipeline stage:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.aggregate( [\n {\n $addFields : {\n \"cosh_output\" : { $cosh : { $degreesToRadians : \"$angle\" } }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">trigonometry</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->$addFields<!-- --> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cosh_output"</span> <!-- -->: <!-- -->{ <!-- -->$cosh<!-- --> <!-- -->: <!-- -->{ <!-- -->$degreesToRadians<!-- --> <!-- -->: <span class="lg-highlight-string">"$angle"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/degreesToRadians/#mongodb-expression-exp.-degreesToRadians">
<code class="leafygreen-ui-1l06pbn">
$degreesToRadians
</code>
</a>
expression converts the
<code class="leafygreen-ui-1l06pbn">
angle
</code>
in degrees to radians.
</p>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5c50782193f833234ba90d85\"),\n \"angle\" : NumberDecimal(\"53.1301023541559787031443874490659\"),\n \"cosh_output\" : NumberDecimal(\"1.461642741099671277595921778079396\")\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5c50782193f833234ba90d85"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"53.1301023541559787031443874490659"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cosh_output"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"1.461642741099671277595921778079396"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Because
<code class="leafygreen-ui-1l06pbn">
angle
</code>
is stored as a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimal
</span>
</a>
, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
output is also a
128-bit decimal.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_c3zrtdznu" class="leafygreen-ui-38lglc" id="_w8xjxn1j1" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
trigonometry
</code>
collection contains a document
that stores an
<code class="leafygreen-ui-1l06pbn">
angle
</code>
value measured in radians:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.insertOne(\n {\n \"_id\" : ObjectId( \"5c50782193f833234ba90d15\" ),\n \"angle\" : NumberDecimal( \"1.6301023541559787031443874490659\" )\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">trigonometry</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>( <span class="lg-highlight-string">"5c50782193f833234ba90d15"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>( <span class="lg-highlight-string">"1.6301023541559787031443874490659"</span> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
expression to calculate the hyperbolic
cosine of
<code class="leafygreen-ui-1l06pbn">
angle
</code>
and adds it to the input document using
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
pipeline stage:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.aggregate( [\n {\n $addFields : {\n \"cosh_output\" : { $cosh : \"$angle\" }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">trigonometry</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->$addFields<!-- --> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cosh_output"</span> <!-- -->: <!-- -->{ <!-- -->$cosh<!-- --> <!-- -->: <span class="lg-highlight-string">"$angle"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5c50782193f833234ba90d15\"),\n \"angle\" : NumberDecimal(\"1.6301023541559787031443874490659\"),\n \"cosh_output\" : NumberDecimal(\"2.650153334504361016712328539738000\")\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5c50782193f833234ba90d15"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"1.6301023541559787031443874490659"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cosh_output"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"2.650153334504361016712328539738000"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Because
<code class="leafygreen-ui-1l06pbn">
angle
</code>
is stored as a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimal
</span>
</a>
, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-cosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</span>
</a>
output is also
a 128-bit decimal.
</p>
</div>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-enterprise-on-red-hat-tarball/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Install MongoDB Enterprise on Red Hat or CentOS using .tgz Tarball
<a class="headerlink leafygreen-ui-1kepx47" href="#install-mongodb-enterprise-on-red-hat-or-centos-using-.tgz-tarball" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="install-mongodb-enterprise-on-red-hat-or-centos-using-.tgz-tarball">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#overview" target="_self">
<span>
Overview
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#considerations" target="_self">
<span>
Considerations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#install-mongodb-enterprise-edition" target="_self">
<span>
Install MongoDB Enterprise Edition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#run-mongodb-enterprise-edition" target="_self">
<span>
Run MongoDB Enterprise Edition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#additional-information" target="_self">
<span>
Additional Information
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Overview
<a class="headerlink leafygreen-ui-1kepx47" href="#overview" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="overview">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use this tutorial to manually install MongoDB 8.0
<!-- -->
Enterprise
<!-- -->
Edition on Red Hat Enterprise Linux, CentOS Linux, or Oracle Linux
<a class="footnote-reference header-buffer" href="#footnote-oracle-linux" id="ref-oracle-linux-id1">
[
<!-- -->
1
<!-- -->
]
</a>
using a downloaded
<code class="leafygreen-ui-1l06pbn">
.tgz
</code>
tarball.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/mongodb-enterprise-advanced?tck=docs_server" target="_self">
<span>
MongoDB Enterprise Edition
</span>
</a>
is available on select platforms and contains support for several
features related to security and monitoring.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
MongoDB Version
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-version" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-version">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This tutorial installs MongoDB 8.0
<!-- -->
Enterprise
<!-- -->
Edition. To install a different version of MongoDB
<!-- -->
Enterprise
<!-- -->
,
use the version drop-down menu in the upper-left corner of this page to
select the documentation for that version.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Installation Method
<a class="headerlink leafygreen-ui-1kepx47" href="#installation-method" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="installation-method">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
While MongoDB can be installed manually via a downloaded
<code class="leafygreen-ui-1l06pbn">
.tgz
</code>
tarball as described in this document, it is recommended to use the
<code class="leafygreen-ui-1l06pbn">
yum
</code>
package manager on your system to install MongoDB if
possible. Using a package manager automatically installs all needed
dependencies, provides an example
<code class="leafygreen-ui-1l06pbn">
mongod.conf
</code>
file to get you
started, and simplifies future upgrade and maintenance tasks.
</p>
<p class="leafygreen-ui-1kp3ins">
➤
<!-- -->
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-red-hat/">
Install MongoDB using the yum Package Manager
</a>
for instructions.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="considerations">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
MongoDB Shell,
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-shell--mongosh" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-shell--mongosh">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When you use the
<code class="leafygreen-ui-1l06pbn">
.tgz
</code>
package to install the server, you need to
follow the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/install/" target="_self">
<span>
mongosh installation instructions
</span>
</a>
to
download and install
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
separately.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Platform Support
<a class="headerlink leafygreen-ui-1kepx47" href="#platform-support" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="platform-support">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 8.0 Enterprise Edition supports the following
<strong class="css-4dei7l">
64-bit
</strong>
versions of Red Hat Enterprise Linux (RHEL), CentOS Linux,
Oracle Linux
<a class="footnote-reference header-buffer" href="#footnote-oracle-linux" id="ref-oracle-linux-id2">
[
<!-- -->
1
<!-- -->
]
</a>
, Rocky Linux, and AlmaLinux
<a class="footnote-reference header-buffer" href="#footnote-rocky-almalinux" id="ref-rocky-almalinux-id3">
[
<!-- -->
2
<!-- -->
]
</a>
on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/production-notes/#std-label-prod-notes-supported-platforms-x86_64">
x86_64
</a>
architecture:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<span class="leafygreen-ui-vm4wms" tabindex="0">
RHEL
</span>
/ CentOS Stream / Oracle / Rocky / AlmaLinux 9
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<span class="leafygreen-ui-vm4wms" tabindex="0">
RHEL
</span>
/ CentOS Stream / Oracle / Rocky / AlmaLinux 8
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
MongoDB only supports the 64-bit versions of these platforms.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB 8.0 Enterprise Edition on
<span class="leafygreen-ui-vm4wms" tabindex="0">
RHEL
</span>
/ CentOS / Oracle / Rocky / Alma
Linux also supports the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/production-notes/#std-label-prod-notes-supported-platforms-ARM64">
ARM64
</a>
architecture on
select platforms.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/production-notes/#std-label-prod-notes-supported-platforms">
Platform Support
</a>
for more information.
</p>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-oracle-linux" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<!-- -->
1
<!-- -->
]
</td>
<td class="leafygreen-ui-17r2xs0">
<em>
(
<a href="#ref-oracle-linux-id1">
1
</a>
,
<a href="#ref-oracle-linux-id2">
2
</a>
)
</em>
<!-- -->
MongoDB only supports Oracle Linux running the Red Hat Compatible
Kernel (RHCK). MongoDB does
<strong>
not
</strong>
support the Unbreakable
Enterprise Kernel (UEK).
</td>
</tr>
</tbody>
</table>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-rocky-almalinux" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-rocky-almalinux-id3">
2
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
MongoDB on-premises products released for RHEL version 8.0+ are
compatible with and supported on Rocky Linux version 8.0+ and
AlmaLinux version 8.0+, contingent upon those distributions meeting
their obligation to deliver full RHEL compatibility.
</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Production Notes
<a class="headerlink leafygreen-ui-1kepx47" href="#production-notes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="production-notes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Before deploying MongoDB in a production environment, consider the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/production-notes/">
Production Notes for Self-Managed Deployments
</a>
document which offers
performance considerations and configuration recommendations for
production MongoDB deployments.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-install-redhat-from-tarball">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Install MongoDB Enterprise Edition
<a class="headerlink leafygreen-ui-1kepx47" href="#install-mongodb-enterprise-edition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="install-mongodb-enterprise-edition">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Prerequisites
<a class="headerlink leafygreen-ui-1kepx47" href="#prerequisites" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="prerequisites">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Use the following command to install the dependencies required for the
MongoDB
<!-- -->
Enterprise
<!-- -->
<code class="leafygreen-ui-1l06pbn">
.tgz
</code>
tarball:
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-13">
<div aria-label="Tabs to describe usage of rhel-centos-6/rhel-centos-8/rhel-centos-9" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_9xqlkpdj3" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_ok4ciaxbs" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
RHEL / CentOS 9
</div>
</button>
<button aria-controls="_4wn8fdhf2" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_nfvzmici1" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
RHEL / CentOS 8
</div>
</button>
<button aria-controls="_1y16g8zbo" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_mg73a6qrc" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
RHEL / CentOS 6
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_323hhktmh" class="" id="_61fv1yvld" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl openldap openssl xz-libs","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl openldap openssl xz-libs</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_fsae8e2qf" class="leafygreen-ui-38lglc" id="_kv299x2cl" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl openldap openssl xz-libs","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl openldap openssl xz-libs</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_hjjjria7o" class="leafygreen-ui-38lglc" id="_crqel0r79" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl openldap openssl xz-libs","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl openldap openssl xz-libs</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Follow these steps to manually install MongoDB
<!-- -->
Enterprise
<!-- -->
Edition from
the
<code class="leafygreen-ui-1l06pbn">
.tgz
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Download the tarball.After you have installed the required prerequisite packages, download\nthe MongoDB Enterprise tgz tarball from the following link:➤ MongoDB Download CenterIn the Version dropdown, select the version of\nMongoDB to download.In the Platform dropdown, select your operating system\nversion and architecture.In the Package dropdown, select tgz.Click Download."},{"@type":"HowToStep","text":"Extract the files from the downloaded archive.For example, from a system shell, you can extract using the tar command:"},{"@type":"HowToStep","text":"Ensure the binaries are in a directory listed in your PATH environment variable.The MongoDB binaries are in the bin/ directory of the tarball.\nYou can either:Copy the binaries into a directory listed in your PATH\nvariable, such as /usr/local/bin (Update\n/path/to/the/mongodb-directory/ with your installation\ndirectory as appropriate)Create symbolic links to the binaries from a directory listed in\nyour PATH variable, such as /usr/local/bin (Update\n/path/to/the/mongodb-directory/ with your installation\ndirectory as appropriate):"},{"@type":"HowToStep","text":"Install the MongoDB Shell (mongosh).Install mongosh then use the MongoDB Shell\nto connect to your deployment.Download the package for the version of mongosh you need from the\nMongoDB Download Center and uncompress the package."}],"name":"Procedure","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Download the tarball.
<a class="headerlink leafygreen-ui-1kepx47" href="#download-the-tarball" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="download-the-tarball">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
After you have installed the required prerequisite packages, download
the MongoDB
<!-- -->
Enterprise
<!-- -->
<code class="leafygreen-ui-1l06pbn">
tgz
</code>
tarball from the following link:
</p>
<p class="leafygreen-ui-1kp3ins">
➤
<!-- -->
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/try/download/enterprise?tck=docs_server" target="_self">
<span>
MongoDB Download Center
</span>
</a>
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In the
<span class="guilabel css-h15tq0">
Version
</span>
dropdown, select the version of
MongoDB to download.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In the
<span class="guilabel css-h15tq0">
Platform
</span>
dropdown, select your operating system
version and architecture.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In the
<span class="guilabel css-h15tq0">
Package
</span>
dropdown, select
<span class="guilabel css-h15tq0">
tgz
</span>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Click
<span class="guilabel css-h15tq0">
Download
</span>
.
</p>
</li>
</ol>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Extract the files from the downloaded archive.
<a class="headerlink leafygreen-ui-1kepx47" href="#extract-the-files-from-the-downloaded-archive" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="extract-the-files-from-the-downloaded-archive">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
For example, from a system shell, you can extract using the
<code class="leafygreen-ui-1l06pbn">
tar
</code>
command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"tar -zxvf mongodb-linux-*-8.0.0.tgz","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">tar -zxvf mongodb-linux-*-8.0.0.tgz</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Ensure the binaries are in a directory listed in your
<code class="leafygreen-ui-1l06pbn">
PATH
</code>
environment variable.
<a class="headerlink leafygreen-ui-1kepx47" href="#ensure-the-binaries-are-in-a-directory-listed-in-your-path-environment-variable" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="ensure-the-binaries-are-in-a-directory-listed-in-your-path-environment-variable">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The MongoDB binaries are in the
<code class="leafygreen-ui-1l06pbn">
bin/
</code>
directory of the tarball.
You can either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Copy the binaries into a directory listed in your
<code class="leafygreen-ui-1l06pbn">
PATH
</code>
variable, such as
<code class="leafygreen-ui-1l06pbn">
/usr/local/bin
</code>
(Update
<code class="leafygreen-ui-1l06pbn">
/path/to/the/mongodb-directory/
</code>
with your installation
directory as appropriate)
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo cp /path/to/the/mongodb-directory/bin/* /usr/local/bin/","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">cp</span> <!-- -->/path/to/the/mongodb-directory/bin<!-- -->/* <!-- -->/usr/local/bin<!-- -->/</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create symbolic links to the binaries from a directory listed in
your
<code class="leafygreen-ui-1l06pbn">
PATH
</code>
variable, such as
<code class="leafygreen-ui-1l06pbn">
/usr/local/bin
</code>
(Update
<code class="leafygreen-ui-1l06pbn">
/path/to/the/mongodb-directory/
</code>
with your installation
directory as appropriate):
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo ln -s /path/to/the/mongodb-directory/bin/* /usr/local/bin/","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">ln</span> -s <!-- -->/path/to/the/mongodb-directory/bin<!-- -->/* <!-- -->/usr/local/bin<!-- -->/</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ul>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
4
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Install the MongoDB Shell (
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
).
<a class="headerlink leafygreen-ui-1kepx47" href="#install-the-mongodb-shell-mongosh" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="install-the-mongodb-shell-mongosh">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/install/" target="_self">
<span>
Install
</span>
</a>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
then use the MongoDB Shell
to connect to your deployment.
</p>
<p class="leafygreen-ui-1kp3ins">
Download the package for the version of
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
you need from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/try/download/enterprise?tck=docs_server" target="_self">
<span>
MongoDB Download Center
</span>
</a>
and uncompress the package.
</p>
</section>
</div>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Run MongoDB Enterprise Edition
<a class="headerlink leafygreen-ui-1kepx47" href="#run-mongodb-enterprise-edition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="run-mongodb-enterprise-edition">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
ulimit
<a class="headerlink leafygreen-ui-1kepx47" href="#ulimit" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="ulimit">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Most Unix-like operating systems limit the system resources that a
process may use. These limits may negatively impact MongoDB operation,
and should be adjusted. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/ulimit/">
UNIX
<code class="leafygreen-ui-1l06pbn">
ulimit
</code>
Settings for Self-Managed Deployments
</a>
for the recommended
settings for your platform.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
ulimit
</code>
value for number of open files is under
<code class="leafygreen-ui-1l06pbn">
64000
</code>
, MongoDB
generates a startup warning.
</p>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Directory Paths
<a class="headerlink leafygreen-ui-1kepx47" href="#directory-paths" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="directory-paths">
</div>
</a>
</h3>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
To Use Default Directories
<a class="headerlink leafygreen-ui-1kepx47" href="#to-use-default-directories" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="to-use-default-directories">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
By default, MongoDB runs using the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
user account and
uses the following default directories:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
/var/lib/mongo
</code>
(the data directory)
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
/var/log/mongodb
</code>
(the log directory)
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Create the MongoDB data and log directories:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo mkdir -p /var/lib/mongo\nsudo mkdir -p /var/log/mongodb","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">mkdir</span> -p <!-- -->/var/lib/mongo</td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">mkdir</span> -p <!-- -->/var/log/mongodb</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
By default, MongoDB runs using the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
user account. Create
a
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
and a
<code class="leafygreen-ui-1l06pbn">
mongodb
</code>
group. Ensure that the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
belongs to the group then set the owner and group of these directories
to
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo chown -R mongod:mongod /var/lib/mongo\nsudo chown -R mongod:mongod /var/log/mongodb","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">chown</span> -R mongod:mongod <!-- -->/var/lib/mongo</td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">chown</span> -R mongod:mongod <!-- -->/var/log/mongodb</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
To Use Non-Default Directories
<a class="headerlink leafygreen-ui-1kepx47" href="#to-use-non-default-directories" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="to-use-non-default-directories">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
To use a data directory and/or log directory other than the default
directories:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create the new directory or directories.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Edit the configuration file
<code class="leafygreen-ui-1l06pbn">
/etc/mongod.conf
</code>
and modify the
following fields accordingly:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.dbPath">
<code class="leafygreen-ui-1l06pbn">
storage.dbPath
</code>
</a>
to specify a new data directory path (e.g.
<code class="leafygreen-ui-1l06pbn">
/some/data/directory
</code>
)
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.path">
<code class="leafygreen-ui-1l06pbn">
systemLog.path
</code>
</a>
to specify a new log file path (e.g.
<code class="leafygreen-ui-1l06pbn">
/some/log/directory/mongod.log
</code>
)
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure that the user running MongoDB has access to the directory or
directories:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo chown -R mongod:mongod <directory>","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">chown</span> -R mongod:mongod <directory></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you change the user that runs the MongoDB process, you
<strong>
must
</strong>
give the new user access to these directories.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Configure SELinux if enforced. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-on-red-hat/#std-label-install-rhel-configure-selinux">
Configure SELinux
<!-- -->
.
</a>
</p>
</li>
</ol>
<span class="leafygreen-ui-1qnjd6d" id="std-label-install-enterprise-tarball-selinux-data-directory">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-install-enterprise-tarball-rhel-configure-selinux">
</span>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Configure SELinux
<a class="headerlink leafygreen-ui-1kepx47" href="#configure-selinux" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="configure-selinux">
</div>
</a>
</h3>
<span class="leafygreen-ui-1qnjd6d" id="std-label-selinux-installation-instructions">
</span>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
An improperly configured SELinux policy might be insecure or may
stop your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance from working.
</p>
<p class="leafygreen-ui-1kp3ins">
If SELinux is in
<code class="leafygreen-ui-1l06pbn">
enforcing
</code>
mode, you must customize your SELinux
policy for MongoDB to
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Permit Access to
<code class="leafygreen-ui-1l06pbn">
cgroup
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Permit Access to
<code class="leafygreen-ui-1l06pbn">
netstat
</code>
</p>
</li>
</ul>
</div>
</div>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Permit Access to
<code class="leafygreen-ui-1l06pbn">
cgroup
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#permit-access-to-cgroup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="permit-access-to-cgroup">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The current SELinux Policy does not allow the MongoDB process to
access
<code class="leafygreen-ui-1l06pbn">
/sys/fs/cgroup
</code>
, which is required to determine
the available memory on your system. If you intend to run SELinux in
<code class="leafygreen-ui-1l06pbn">
enforcing
</code>
mode, you will need to make the following adjustment
to your SELinux policy:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure your system has the
<code class="leafygreen-ui-1l06pbn">
checkpolicy
</code>
package installed:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo yum install checkpolicy","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo yum install checkpolicy</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create a custom policy file
<code class="file docutils literal">
<span class="pre">
mongodb_cgroup_memory.te
</span>
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cat > mongodb_cgroup_memory.te <","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-built_in">cat</span> > mongodb_cgroup_memory.te <!-- --><<<span class="lg-highlight-string">EOF</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">module mongodb_cgroup_memory 1.0;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">require {</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type cgroup_t;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type mongod_t;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> class dir search;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> class file { getattr open read };</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">#============= mongod_t ==============</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t cgroup_t:dir search;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t cgroup_t:file { getattr open read };</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">EOF</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Once created, compile and load the custom policy module by
running these three commands:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"checkmodule -M -m -o mongodb_cgroup_memory.mod mongodb_cgroup_memory.te\nsemodule_package -o mongodb_cgroup_memory.pp -m mongodb_cgroup_memory.mod\nsudo semodule -i mongodb_cgroup_memory.pp","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">checkmodule -M -m -o mongodb_cgroup_memory.mod mongodb_cgroup_memory.te</td></tr><tr class=""><td class="leafygreen-ui-7razhx">semodule_package -o mongodb_cgroup_memory.pp -m mongodb_cgroup_memory.mod</td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo semodule -i mongodb_cgroup_memory.pp</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
The MongoDB process is now able to access the correct files with
SELinux set to
<code class="leafygreen-ui-1l06pbn">
enforcing
</code>
.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Permit Access to
<code class="leafygreen-ui-1l06pbn">
netstat
</code>
for FTDC
<a class="headerlink leafygreen-ui-1kepx47" href="#permit-access-to-netstat-for-ftdc" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="permit-access-to-netstat-for-ftdc">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The current SELinux Policy does not allow the MongoDB process to open
and read
<code class="leafygreen-ui-1l06pbn">
/proc/net/netstat
</code>
, which is required for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/full-time-diagnostic-data-capture/#std-label-ftdc-stub">
Full Time Diagnostic Data Capture (FTDC)
</a>
.
If you intend to run SELinux in
<code class="leafygreen-ui-1l06pbn">
enforcing
</code>
mode, you will need to make the following adjustment
to your SELinux policy:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure your system has the
<code class="leafygreen-ui-1l06pbn">
checkpolicy
</code>
package installed:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo yum install checkpolicy","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo yum install checkpolicy</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create a custom policy file
<code class="file docutils literal">
<span class="pre">
mongodb_proc_net.te
</span>
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cat > mongodb_proc_net.te <","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-built_in">cat</span> > mongodb_proc_net.te <!-- --><<<span class="lg-highlight-string">EOF</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">module mongodb_proc_net 1.0;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">require {</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type cgroup_t;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type configfs_t;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type file_type;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type mongod_t;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type proc_net_t;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type sysctl_fs_t;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> type var_lib_nfs_t;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> class dir { search getattr };</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> class file { getattr open read };</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">#============= mongod_t ==============</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t cgroup_t:dir { search getattr } ;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t cgroup_t:file { getattr open read };</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t configfs_t:dir getattr;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t file_type:dir { getattr search };</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t file_type:file getattr;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t proc_net_t:file { open read };</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t sysctl_fs_t:dir search;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">allow mongod_t var_lib_nfs_t:dir search;</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">EOF</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Once created, compile and load the custom policy module by
running these three commands:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"checkmodule -M -m -o mongodb_proc_net.mod mongodb_proc_net.te\nsemodule_package -o mongodb_proc_net.pp -m mongodb_proc_net.mod\nsudo semodule -i mongodb_proc_net.pp","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">checkmodule -M -m -o mongodb_proc_net.mod mongodb_proc_net.te</td></tr><tr class=""><td class="leafygreen-ui-7razhx">semodule_package -o mongodb_proc_net.pp -m mongodb_proc_net.mod</td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo semodule -i mongodb_proc_net.pp</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ol>
<span class="leafygreen-ui-1qnjd6d" id="std-label-selinux-custom-policy-howto">
</span>
<section>
<h5 class="contains-headerlink leafygreen-ui-1vzlt5f">
Using a Custom MongoDB Directory Path
<a class="headerlink leafygreen-ui-1kepx47" href="#using-a-custom-mongodb-directory-path" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="using-a-custom-mongodb-directory-path">
</div>
</a>
</h5>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update the SELinux policy to allow the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
service
to use the new directory:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo semanage fcontext -a -t <type> ","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo semanage fcontext -a -t <<span class="lg-highlight-built_in">type</span>> <<!-- -->/some<!-- -->/MongoDB<!-- -->/directory.<!-- -->*></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Specify one of the following types as appropriate:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mongod_var_lib_t
</code>
for data directory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mongod_log_t
</code>
for log file directory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mongod_var_run_t
</code>
for pid file directory
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Be sure to include the
<code class="leafygreen-ui-1l06pbn">
.*
</code>
at the end of the directory.
</p>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update the SELinux user policy for the new directory:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo chcon -Rv -u system_u -t <type> ","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">chcon</span> -Rv -u system_u -t <<span class="lg-highlight-built_in">type</span>> <<!-- -->/some<!-- -->/MongoDB<!-- -->/directory<!-- -->></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Specify one of the following types as appropriate:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mongod_var_lib_t
</code>
for data directory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mongod_log_t
</code>
for log directory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mongod_var_run_t
</code>
for pid file directory
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Apply the updated SELinux policies to the directory:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo restorecon -R -v ","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo restorecon -R -v <<!-- -->/some<!-- -->/MongoDB<!-- -->/directory<!-- -->></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
For example:
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Be sure to include the
<code class="leafygreen-ui-1l06pbn">
.*
</code>
at the end of the directory for the
<code class="leafygreen-ui-1l06pbn">
semanage fcontext
</code>
operations.
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If using a non-default MongoDB data path of
<code class="leafygreen-ui-1l06pbn">
/mongodb/data
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo semanage fcontext -a -t mongod_var_lib_t '/mongodb/data.*'\nsudo chcon -Rv -u system_u -t mongod_var_lib_t '/mongodb/data'\nsudo restorecon -R -v '/mongodb/data'","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo semanage fcontext -a -t mongod_var_lib_t <span class="lg-highlight-string">'/mongodb/data.*'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">chcon</span> -Rv -u system_u -t mongod_var_lib_t <span class="lg-highlight-string">'/mongodb/data'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo restorecon -R -v <span class="lg-highlight-string">'/mongodb/data'</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If using a non-default MongoDB log directory of
<code class="leafygreen-ui-1l06pbn">
/mongodb/log
</code>
(e.g. if the log file path is
<code class="leafygreen-ui-1l06pbn">
/mongodb/log/mongod.log
</code>
):
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo semanage fcontext -a -t mongod_log_t '/mongodb/log.*'\nsudo chcon -Rv -u system_u -t mongod_log_t '/mongodb/log'\nsudo restorecon -R -v '/mongodb/log'","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo semanage fcontext -a -t mongod_log_t <span class="lg-highlight-string">'/mongodb/log.*'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">chcon</span> -Rv -u system_u -t mongod_log_t <span class="lg-highlight-string">'/mongodb/log'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo restorecon -R -v <span class="lg-highlight-string">'/mongodb/log'</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ul>
</section>
<section>
<h5 class="contains-headerlink leafygreen-ui-1vzlt5f">
Using a Custom MongoDB Port
<a class="headerlink leafygreen-ui-1kepx47" href="#using-a-custom-mongodb-port" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="using-a-custom-mongodb-port">
</div>
</a>
</h5>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo semanage port -a -t mongod_port_t -p tcp <portnumber>","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo semanage port -a -t mongod_port_t -p tcp <portnumber></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
In addition to the above, if SELinux is in
<code class="leafygreen-ui-1l06pbn">
enforcing
</code>
mode you
will also need to further customize your SELinux policy for each of
these situations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You are using a
<strong>
custom directory path
</strong>
instead of using the
default path for any combination of:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.dbPath">
<code class="leafygreen-ui-1l06pbn">
dbPath
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.path">
<code class="leafygreen-ui-1l06pbn">
systemLog.path
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-processManagement.pidFilePath">
<code class="leafygreen-ui-1l06pbn">
pidFilePath
</code>
</a>
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You are using a
<strong>
custom port
</strong>
instead of using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/default-mongodb-port/">
default
MongoDB port
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you have made other modifications to your MongoDB installation.
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure-1" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure-1">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Follow these steps to run MongoDB
<!-- -->
Enterprise
<!-- -->
Edition on your system.
These instructions assume that you are using the default settings.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Create the data and log directories.Create a directory where the MongoDB instance stores its data. For\nexample:Create a directory where the MongoDB instance stores its log. For example:The user that starts the MongoDB process must have read and write\npermission to these directories. For example, if you intend to run\nMongoDB as yourself:"},{"@type":"HowToStep","text":"Run MongoDB.To run MongoDB, run the mongod process at the system prompt.For details on the command-line options --dbpath and --logpath, see\nOptions."},{"@type":"HowToStep","text":"Verify that MongoDB has started successfully.Verify that MongoDB has started successfully by\nchecking the process output for the following line in the\nlog file /var/log/mongodb/mongod.log:You may see non-critical warnings in the process\noutput. As long as you see the log line shown above, you can safely\nignore these warnings during your initial evaluation of MongoDB."},{"@type":"HowToStep","text":"Begin using MongoDB.Start a mongosh session on the same host machine as the\nmongod. You can run mongosh\nwithout any command-line options to connect to a\nmongod that is running on your localhost with default\nport 27017.For more information on connecting using mongosh,\nsuch as to connect to a mongod instance running\non a different host and/or port, see the\nmongosh documentation.To help you start using MongoDB, MongoDB provides Getting\nStarted Guides in various driver editions. For the\ndriver documentation, see Start Developing with MongoDB."}],"name":"Procedure","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Create the data and log directories.
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-data-and-log-directories" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-the-data-and-log-directories">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Create a directory where the MongoDB instance stores its data. For
example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo mkdir -p /var/lib/mongo","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">mkdir</span> -p <!-- -->/var/lib/mongo</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Create a directory where the MongoDB instance stores its log. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo mkdir -p /var/log/mongodb","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">mkdir</span> -p <!-- -->/var/log/mongodb</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The user that starts the MongoDB process must have read and write
permission to these directories. For example, if you intend to run
MongoDB as yourself:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sudo chown `whoami` /var/lib/mongo # Or substitute another user\nsudo chown `whoami` /var/log/mongodb # Or substitute another user","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">chown</span> `<span class="lg-highlight-built_in">whoami</span>` <!-- -->/var/lib/mongo<!-- --> <span class="lg-highlight-comment"># Or substitute another user</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">sudo <span class="lg-highlight-built_in">chown</span> `<span class="lg-highlight-built_in">whoami</span>` <!-- -->/var/log/mongodb<!-- --> <span class="lg-highlight-comment"># Or substitute another user</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Run MongoDB.
<a class="headerlink leafygreen-ui-1kepx47" href="#run-mongodb" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="run-mongodb">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
To run MongoDB, run the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
process at the system prompt.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --dbpath <!-- -->/var/lib/mongo<!-- --> --logpath <!-- -->/var/log/mongodb/mongod.log<!-- --> --fork</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For details on the command-line options
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--logpath">
<code class="leafygreen-ui-1l06pbn">
--logpath
</code>
</a>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-label-mongod-options">
Options
<!-- -->
.
</a>
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Verify that MongoDB has started successfully.
<a class="headerlink leafygreen-ui-1kepx47" href="#verify-that-mongodb-has-started-successfully" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="verify-that-mongodb-has-started-successfully">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Verify that MongoDB has started successfully by
checking the process output for the following line in the
log file
<code class="leafygreen-ui-1l06pbn">
/var/log/mongodb/mongod.log
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[initandlisten] waiting for connections on port 27017\n"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1agdxuk" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[initandlisten] waiting for connections on port 27017</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You may see non-critical warnings in the process
output. As long as you see the log line shown above, you can safely
ignore these warnings during your initial evaluation of MongoDB.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
4
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Begin using MongoDB.
<a class="headerlink leafygreen-ui-1kepx47" href="#begin-using-mongodb" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="begin-using-mongodb">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Start a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
session on the same host machine as the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
. You can run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
without any command-line options to connect to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
that is running on your localhost with default
port 27017.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\nmongosh\n","programmingLanguage":"Shell"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongosh</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on connecting using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
,
such as to connect to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance running
on a different host and/or port, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh documentation
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To help you start using MongoDB, MongoDB provides
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/getting-started/#std-label-getting-started">
Getting
Started Guides
</a>
in various driver editions. For the
driver documentation, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://api.mongodb.com/" target="_self">
<span>
Start Developing with MongoDB
<!-- -->
.
</span>
</a>
</p>
</section>
</div>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Additional Information
<a class="headerlink leafygreen-ui-1kepx47" href="#additional-information" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="additional-information">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Localhost Binding by Default
<a class="headerlink leafygreen-ui-1kepx47" href="#localhost-binding-by-default" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="localhost-binding-by-default">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
By default, MongoDB launches with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.bindIp">
<code class="leafygreen-ui-1l06pbn">
bindIp
</code>
</a>
set to
<code class="leafygreen-ui-1l06pbn">
127.0.0.1
</code>
, which binds to the localhost network interface. This
means that the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
can only accept connections from
clients that are running on the same machine. Remote clients will not be
able to connect to the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
, and the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
will
not be able to initialize a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
</a>
unless this value is set
to a valid network interface.
</p>
<p class="leafygreen-ui-1kp3ins">
This value can be configured either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
in the MongoDB configuration file with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.bindIp">
<code class="leafygreen-ui-1l06pbn">
bindIp
</code>
</a>
, or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
via the command-line argument
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--bind_ip">
<code class="leafygreen-ui-1l06pbn">
--bind_ip
</code>
</a>
</p>
</li>
</ul>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before you bind your instance to a publicly-accessible IP address,
you must secure your cluster from unauthorized access. For a complete
list of security recommendations, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-security-checklist">
Security Checklist for Self-Managed Deployments
</a>
. At minimum, consider
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-checklist-auth">
enabling authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-hardening/#std-label-network-config-hardening">
hardening
network infrastructure
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on configuring
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.bindIp">
<code class="leafygreen-ui-1l06pbn">
bindIp
</code>
</a>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-mongodb-configuration/">
IP Binding in Self-Managed Deployments
<!-- -->
.
</a>
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/read-isolation-consistency-recency/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Read Isolation, Consistency, and Recency
<a class="headerlink leafygreen-ui-1kepx47" href="#read-isolation--consistency--and-recency" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="read-isolation--consistency--and-recency">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#isolation-guarantees" target="_self">
<span>
Isolation Guarantees
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#monotonic-writes" target="_self">
<span>
Monotonic Writes
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#real-time-order" target="_self">
<span>
Real Time Order
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#causal-consistency" target="_self">
<span>
Causal Consistency
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Isolation Guarantees
<a class="headerlink leafygreen-ui-1kepx47" href="#isolation-guarantees" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="isolation-guarantees">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Read Uncommitted
<a class="headerlink leafygreen-ui-1kepx47" href="#read-uncommitted" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="read-uncommitted">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Depending on the read concern, clients can see the results of writes
before the writes are
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-durable">
durable
<!-- -->
:
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Regardless of a write's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
write concern
</a>
, other
clients using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-local/#mongodb-readconcern-readconcern.-local-">
<code class="leafygreen-ui-1l06pbn">
"local"
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-available/#mongodb-readconcern-readconcern.-available-">
<code class="leafygreen-ui-1l06pbn">
"available"
</code>
</a>
read concern can see the result of a write operation before the write
operation is acknowledged to the issuing client.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Clients using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-local/#mongodb-readconcern-readconcern.-local-">
<code class="leafygreen-ui-1l06pbn">
"local"
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-available/#mongodb-readconcern-readconcern.-available-">
<code class="leafygreen-ui-1l06pbn">
"available"
</code>
</a>
read concern can read data which may be subsequently
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-rollbacks/">
rolled
back
</a>
during replica set failovers.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For operations in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/">
multi-document transaction
</a>
, when a transaction commits, all data changes
made in the transaction are saved and visible outside the transaction.
That is, a transaction will not commit some of its changes while
rolling back others.
</p>
<p class="leafygreen-ui-1kp3ins">
Until a transaction commits, the data changes made in the
transaction are not visible outside the transaction.
</p>
<p class="leafygreen-ui-1kp3ins">
However, when a transaction writes to multiple shards, not all
outside read operations need to wait for the result of the committed
transaction to be visible across the shards. For example, if a
transaction is committed and write 1 is visible on shard A but write
2 is not yet visible on shard B, an outside read at read concern
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-local/#mongodb-readconcern-readconcern.-local-">
<code class="leafygreen-ui-1l06pbn">
"local"
</code>
</a>
can read the results of write 1 without
seeing write 2.
</p>
<p class="leafygreen-ui-1kp3ins">
Read uncommitted is the default isolation level and applies to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
standalone instances as well as to replica sets and
sharded clusters.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Read Uncommitted And Single Document Atomicity
<a class="headerlink leafygreen-ui-1kepx47" href="#read-uncommitted-and-single-document-atomicity" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="read-uncommitted-and-single-document-atomicity">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Write operations are atomic with respect to a single document; i.e.
if a write is updating multiple fields in the document, a read operation
will never see the document with only some of the fields updated.
However, although a client may not see a
<em>
partially
</em>
updated
document, read uncommitted means that concurrent read operations may still
see the updated document before the changes are made
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-durable">
durable
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
With a standalone
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance, a set of read and write
operations to a single document is serializable. With a replica set,
a set of read and write operations to a single document is serializable
<em>
only
</em>
in the absence of a rollback.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Read Uncommitted And Multiple Document Write
<a class="headerlink leafygreen-ui-1kepx47" href="#read-uncommitted-and-multiple-document-write" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="read-uncommitted-and-multiple-document-write">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When a single write operation (e.g.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateMany/#mongodb-method-db.collection.updateMany">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</a>
) modifies multiple documents,
the modification of each document is atomic, but the operation as a
whole is not atomic.
</p>
<p class="leafygreen-ui-1kp3ins">
When performing multi-document write operations, whether through a
single write operation or multiple write operations, other
operations may interleave.
</p>
<p class="leafygreen-ui-1kp3ins">
For situations that require atomicity of reads and writes to multiple
documents (in a single or multiple collections), MongoDB supports
distributed transactions, including transactions on replica sets and
sharded clusters.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
transactions
</a>
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
In most cases, a distributed transaction incurs a greater
performance cost over single document writes, and the
availability of distributed transactions should not be a
replacement for effective schema design. For many scenarios, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/concepts/embedding-vs-references/#std-label-data-modeling-embedding">
denormalized data model (embedded documents and arrays)
</a>
will continue to be optimal for your
data and use cases. That is, for many scenarios, modeling your data
appropriately will minimize the need for distributed
transactions.
</p>
<p class="leafygreen-ui-1kp3ins">
For additional transactions usage considerations
(such as runtime limit and oplog size limit), see also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions-production-consideration/#std-label-production-considerations">
Production Considerations
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Without isolating the multi-document write operations, MongoDB exhibits
the following behavior:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Non-point-in-time read operations. Suppose a read operation
begins at time
<em>
t
</em>
<sub>
1
</sub>
and starts reading documents. A
write operation then commits an update to one of the documents at
some later time
<em>
t
</em>
<sub>
2
</sub>
. The reader may see the updated
version of the document, and therefore does not see a
point-in-time snapshot of the data.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Non-serializable operations. Suppose a read operation reads a
document
<em>
d
</em>
<sub>
1
</sub>
at time
<em>
t
</em>
<sub>
1
</sub>
and a write
operation updates
<em>
d
</em>
<sub>
1
</sub>
at some later time
<em>
t
</em>
<sub>
3
</sub>
.
This introduces a read-write dependency such that, if the
operations were to be serialized, the read operation must precede
the write operation. But also suppose that the write operation
updates document
<em>
d
</em>
<sub>
2
</sub>
at time
<em>
t
</em>
<sub>
2
</sub>
and the read
operation subsequently reads
<em>
d
</em>
<sub>
2
</sub>
at some later time
<em>
t
</em>
<sub>
4
</sub>
. This introduces a write-read dependency which
would instead require the read operation to come
<em>
after
</em>
the
write operation in a serializable schedule. There is a dependency
cycle which makes serializability impossible.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reads may miss matching documents that are updated during the
course of the read operation.
</p>
</li>
</ol>
<span class="leafygreen-ui-1qnjd6d" id="std-label-faq-developers-isolate-cursors">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Cursor Snapshot
<a class="headerlink leafygreen-ui-1kepx47" href="#cursor-snapshot" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="cursor-snapshot">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB cursors can return the same document more than once in some
situations. As a cursor returns documents, other operations may
interleave with the query. If one of these operations changes the
indexed field on the index used by the query, then the cursor could
return the same document more than once.
</p>
<p class="leafygreen-ui-1kp3ins">
Queries that use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
unique indexes
</a>
can, in
some cases, return duplicate values. If a cursor using a unique index
interleaves with a delete and insert of documents sharing the same
unique value, the cursor may return the same unique value twice from
different documents.
</p>
<p class="leafygreen-ui-1kp3ins">
Consider using read isolation. To learn more, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-snapshot/#std-label-read-concern-snapshot">
Read Concern
<code class="leafygreen-ui-1l06pbn">
"snapshot"
</code>
.
</a>
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Monotonic Writes
<a class="headerlink leafygreen-ui-1kepx47" href="#monotonic-writes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="monotonic-writes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB provides monotonic write guarantees, by default, for
standalone
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances and replica set.
</p>
<p class="leafygreen-ui-1kp3ins">
For monotonic writes and sharded clusters, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-causal-consistency" target="_self">
<span>
Causal Consistency
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-consistency-linearizable-reads">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Real Time Order
<a class="headerlink leafygreen-ui-1kepx47" href="#real-time-order" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="real-time-order">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For read and write operations on the primary, issuing read operations
with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</a>
read concern and write operations
with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern enables multiple threads
to perform reads and writes on a single document as if a single thread
performed these operations in real time; that is, the corresponding
schedule for these reads and writes is considered linearizable.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-causal-consistency" target="_self">
<span>
Causal Consistency
</span>
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-causal-consistency">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Causal Consistency
<a class="headerlink leafygreen-ui-1kepx47" href="#causal-consistency" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="causal-consistency">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If an operation logically depends on a preceding operation, there is a
causal relationship between the operations. For example, a write
operation that deletes all documents based on a specified condition and
a subsequent read operation that verifies the delete operation have a
causal relationship.
</p>
<p class="leafygreen-ui-1kp3ins">
With causally consistent sessions, MongoDB executes causal operations
in an order that respect their causal relationships, and clients
observe results that are consistent with the causal relationships.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sessions">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Client Sessions and Causal Consistency Guarantees
<a class="headerlink leafygreen-ui-1kepx47" href="#client-sessions-and-causal-consistency-guarantees" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="client-sessions-and-causal-consistency-guarantees">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To provide causal consistency, MongoDB enables causal consistency
in client sessions. A causally consistent session denotes that the
associated sequence of read operations with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
read concern and write operations with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write
concern have a causal relationship that is reflected by their ordering.
<strong class="css-4dei7l">
Applications must ensure that only one thread at a time executes these
operations in a client session.
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
For causally related operations:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A client starts a client session.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Client sessions only guarantee causal consistency for:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Read operations with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
; i.e. the return
data has been acknowledged by a majority of the replica set
members and is durable.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Write operations with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern;
i.e. the write operations that request acknowledgment that the
operation has been applied to a majority of the replica set's
voting members.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For more information on causal consistency and various read and
write concerns, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/causal-consistency-read-write-concerns/">
Causal Consistency and Read and Write Concerns
<!-- -->
.
</a>
</p>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
As the client issues a sequence of read with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
read concern and write operations (with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern), the client includes the
session information with each operation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For each read operation with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
read concern
and write operation with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern
associated with the session, MongoDB returns the operation time and
the cluster time, even if the operation errors. The client session
keeps track of the operation time and the cluster time.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
MongoDB does not return the operation time and the cluster time
for
<em>
unacknowledged
</em>
(
<code class="leafygreen-ui-1l06pbn">
w: 0
</code>
) write operations. Unacknowledged
writes do not imply any causal relationship.
</p>
<p class="leafygreen-ui-1kp3ins">
Although, MongoDB returns the operation time and the cluster time
for read operations and acknowledged write operations in a client
session, only the read operations with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
read concern and write operations with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern can guarantee causal
consistency. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/causal-consistency-read-write-concerns/">
Causal Consistency and Read and Write Concerns
<!-- -->
.
</a>
</p>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The associated client session tracks these two time fields.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Operations can be causally consistent across different sessions.
MongoDB drivers and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
provide the
methods to advance the operation time and the cluster time for a
client session. So, a client can advance the cluster time and the
operation time of one client session to be consistent with the
operations of another client session.
</p>
</div>
</div>
</li>
</ol>
<span class="leafygreen-ui-1qnjd6d" id="std-label-causal-consistency-guarantees">
</span>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Causal Consistency Guarantees
<a class="headerlink leafygreen-ui-1kepx47" href="#causal-consistency-guarantees" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="causal-consistency-guarantees">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following table lists the causal consistency guarantees provided by
causally consistent sessions for read operations with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
read concern and
write operations with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:22%"/>
<col style="width:78%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Guarantees
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Read your writes
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Read operations reflect the results of write
operations that precede them.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Monotonic reads
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Read operations do not return results that correspond to an
earlier state of the data than a preceding read operation.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, if in a session:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
write
<sub>
1
</sub>
precedes write
<sub>
2
</sub>
,
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
read
<sub>
1
</sub>
precedes read
<sub>
2
</sub>
, and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
read
<sub>
1
</sub>
returns results that reflect write
<sub>
2
</sub>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
then read
<sub>
2
</sub>
cannot return results of write
<sub>
1
</sub>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Monotonic writes
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Write operations that must precede other writes are executed
before those other writes.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, if write
<sub>
1
</sub>
must precede write
<sub>
2
</sub>
in
a session, the state of the data at the time of write
<sub>
2
</sub>
must reflect the state of the data post write
<sub>
1
</sub>
. Other
writes can interleave between write
<sub>
1
</sub>
and write write
<sub>
2
</sub>
, but write
<sub>
2
</sub>
cannot occur before write
<sub>
1
</sub>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Writes follow reads
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Write operations that must occur after read operations are
executed after those read operations. That is, the state of the
data at the time of the write must incorporate the state of the
data of the preceding read operations.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Read Preference
<a class="headerlink leafygreen-ui-1kepx47" href="#read-preference" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="read-preference">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
These guarantees hold across all members of the MongoDB deployment. For
example, if, in a causally consistent session, you issue a write with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern followed by a read that reads
from a secondary (i.e. read preference
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-secondary">
<code class="leafygreen-ui-1l06pbn">
secondary
</code>
</a>
) with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
read concern, the read operation will reflect
the state of the database after the write operation.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Isolation
<a class="headerlink leafygreen-ui-1kepx47" href="#isolation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="isolation">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Operations within a causally consistent session are not isolated from
operations outside the session. If a concurrent write operation
interleaves between the session's write and read operations, the
session's read operation may return results that reflect a write
operation that occurred
<em>
after
</em>
the session's write operation.
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
MongoDB Drivers
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-drivers" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-drivers">
</div>
</a>
</h3>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Applications must ensure that only one thread at a time executes
these operations in a client session.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Clients require MongoDB drivers updated for MongoDB 3.6 or later:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<thead class="leafygreen-ui-a9ho40">
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Java 3.6+
</p>
<p class="leafygreen-ui-1kp3ins">
Python 3.6+
</p>
<p class="leafygreen-ui-1kp3ins">
C 1.9+
</p>
<p class="leafygreen-ui-1kp3ins">
Go 1.8+
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
C# 2.5+
</p>
<p class="leafygreen-ui-1kp3ins">
Node 3.0+
</p>
<p class="leafygreen-ui-1kp3ins">
Ruby 2.5+
</p>
<p class="leafygreen-ui-1kp3ins">
Rust 2.1+
</p>
<p class="leafygreen-ui-1kp3ins">
Swift 1.2+
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Perl 2.0+
</p>
<p class="leafygreen-ui-1kp3ins">
PHPC 1.4+
</p>
<p class="leafygreen-ui-1kp3ins">
Scala 2.2+
</p>
<p class="leafygreen-ui-1kp3ins">
C++ 3.6.6+
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-causal-consistency-examples">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h3>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Causally consistent
sessions can only guarantee causal consistency for reads with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
read concern and writes with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Consider a collection
<code class="leafygreen-ui-1l06pbn">
items
</code>
that maintains the current and
historical data for various items. Only the historical data has a
non-null
<code class="leafygreen-ui-1l06pbn">
end
</code>
date. If the
<code class="leafygreen-ui-1l06pbn">
sku
</code>
value for an item changes, the
document with the old
<code class="leafygreen-ui-1l06pbn">
sku
</code>
value needs to be updated with the
<code class="leafygreen-ui-1l06pbn">
end
</code>
date, after which the new document is inserted with the current
<code class="leafygreen-ui-1l06pbn">
sku
</code>
value. The client can use a causally consistent session to
ensure that the update occurs before the insert.
</p>
<hr class="css-1unva0n e4ru7ww0"/>
<p class="leafygreen-ui-1kp3ins">
➤
<!-- -->
Use the
<strong>
Select your language
</strong>
drop-down menu in the
upper-right to set the language of this example.
</p>
<hr class="css-1unva0n e4ru7ww0"/>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-f2qztv" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-18">
<div aria-label="Tabs to describe usage of drivers" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_4iemvbfd8" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_mugqx0kbl" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
C
</div>
</button>
<button aria-controls="_asof9hkrn" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_821oe15pt" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C#
</div>
</button>
<button aria-controls="_82vt9cs7u" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_tgez3j8e4" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Java (Sync)
</div>
</button>
<button aria-controls="_l6us2b8jh" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_h3isftuso" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Motor
</div>
</button>
<button aria-controls="_0fs0zymbq" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_3piaoxekz" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Perl
</div>
</button>
<button aria-controls="_xt17g9tg8" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_6szc5tvsq" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
PHP
</div>
</button>
<button aria-controls="_hozg0ctqo" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_mjdab4zd1" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Python
</div>
</button>
<button aria-controls="_4yoibi3jt" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_w498to7v8" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Async)
</div>
</button>
<button aria-controls="_tsxy9fe5t" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_z5ktoho2j" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Sync)
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_702bfzpxs" class="" id="_7y8sn1z5b" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\n /* Use a causally-consistent session to run some operations. */\n\n wc = mongoc_write_concern_new ();\n mongoc_write_concern_set_wmajority (wc, 1000);\n mongoc_collection_set_write_concern (coll, wc);\n\n rc = mongoc_read_concern_new ();\n mongoc_read_concern_set_level (rc, MONGOC_READ_CONCERN_LEVEL_MAJORITY);\n mongoc_collection_set_read_concern (coll, rc);\n\n session_opts = mongoc_session_opts_new ();\n mongoc_session_opts_set_causal_consistency (session_opts, true);\n\n session1 = mongoc_client_start_session (client, session_opts, &error);\n if (!session1) {\n fprintf (stderr, \"couldn't start session: %s\\n\", error.message);\n goto cleanup;\n }\n\n /* Run an update_one with our causally-consistent session. */\n update_opts = bson_new ();\n res = mongoc_client_session_append (session1, update_opts, &error);\n if (!res) {\n fprintf (stderr, \"couldn't add session to opts: %s\\n\", error.message);\n goto cleanup;\n }\n\n query = BCON_NEW (\"sku\", \"111\");\n update = BCON_NEW (\"$set\", \"{\", \"end\",\n BCON_DATE_TIME (bson_get_monotonic_time ()), \"}\");\n res = mongoc_collection_update_one (coll,\n\t\t query,\n\t\t update,\n\t\t update_opts,\n\t\t NULL, /* reply */\n\t\t &error);\n\n if (!res) {\n fprintf (stderr, \"update failed: %s\\n\", error.message);\n goto cleanup;\n }\n\n /* Run an insert with our causally-consistent session */\n insert_opts = bson_new ();\n res = mongoc_client_session_append (session1, insert_opts, &error);\n if (!res) {\n fprintf (stderr, \"couldn't add session to opts: %s\\n\", error.message);\n goto cleanup;\n }\n\n insert = BCON_NEW (\"sku\", \"nuts-111\", \"name\", \"Pecans\",\n \"start\", BCON_DATE_TIME (bson_get_monotonic_time ()));\n res = mongoc_collection_insert_one (coll, insert, insert_opts, NULL, &error);\n if (!res) {\n fprintf (stderr, \"insert failed: %s\\n\", error.message);\n goto cleanup;\n }\n","programmingLanguage":"C"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">/*</span><span class="lg-highlight-comment"> Use a causally-consistent </span><span class="lg-highlight-comment">session</span><span class="lg-highlight-comment"> to run some </span><span class="lg-highlight-comment">operations. */</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> wc = mongoc_write_concern_new ();<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_write_concern_set_wmajority (wc, <!-- --><!-- --><span class="lg-highlight-number">1000</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_collection_set_write_concern (coll, wc);<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> rc = mongoc_read_concern_new ();<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_read_concern_set_level (rc, MONGOC_READ_CONCERN_LEVEL_MAJORITY);<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_collection_set_read_concern (coll, rc);<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session_opts = mongoc_session_opts_new ();<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_session_opts_set_causal_consistency (session_opts, <!-- --><!-- --><span class="lg-highlight-literal">true</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session1 = mongoc_client_start_session (client, session_opts, &error);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> (!session1) {<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stderr, <span class="lg-highlight-string">"couldn't start session: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">goto</span> cleanup;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">/* Run an update_one</span><span class="lg-highlight-comment"> with our causally-consistent </span><span class="lg-highlight-comment">session. */</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> update_opts = bson_new ();<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> res = mongoc_client_session_append (session1, update_opts, &error);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> (!res) {<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stderr, <span class="lg-highlight-string">"couldn't add session to opts: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">goto</span> cleanup;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> query = BCON_NEW (<!-- --><!-- --><span class="lg-highlight-string">"sku"</span>, <span class="lg-highlight-string">"111"</span>);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> update = BCON_NEW (<!-- --><!-- --><span class="lg-highlight-string">"$set"</span>, <span class="lg-highlight-string">"{"</span>, <span class="lg-highlight-string">"end"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> BCON_DATE_TIME (bson_get_monotonic_time ()), <!-- --><!-- --><span class="lg-highlight-string">"}"</span>);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> res = mongoc_collection_update_one (coll,<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> query,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> update,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> update_opts,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> NULL, <span class="lg-highlight-comment">/* reply */</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> &error);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> (!res) {<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stderr, <span class="lg-highlight-string">"update failed: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">goto</span> cleanup;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">/* Run an</span><span class="lg-highlight-comment"> insert with our </span><span class="lg-highlight-comment">causally-consistent session */</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> insert_opts = bson_new ();<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> res = mongoc_client_session_append (session1, insert_opts, &error);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> (!res) {<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stderr, <span class="lg-highlight-string">"couldn't add session to opts: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">goto</span> cleanup;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> insert = BCON_NEW (<!-- --><!-- --><span class="lg-highlight-string">"sku"</span>, <span class="lg-highlight-string">"nuts-111"</span>, <span class="lg-highlight-string">"name"</span>, <span class="lg-highlight-string">"Pecans"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"start"</span>, BCON_DATE_TIME (bson_get_monotonic_time ()));<!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> res = mongoc_collection_insert_one (coll, insert, insert_opts, NULL, &error);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> (!res) {<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stderr, <span class="lg-highlight-string">"insert failed: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">goto</span> cleanup;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-21-menu" aria-describedby="select-21-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-22" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_049y10o04" class="leafygreen-ui-38lglc" id="_4vcb2vy1a" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"using (var session1 = client.StartSession(new ClientSessionOptions { CausalConsistency = true }))\n{\n var currentDate = DateTime.UtcNow.Date;\n var items = client.GetDatabase(\n \"test\",\n new MongoDatabaseSettings\n {\n ReadConcern = ReadConcern.Majority,\n WriteConcern = new WriteConcern(\n WriteConcern.WMode.Majority,\n TimeSpan.FromMilliseconds(1000))\n })\n .GetCollection<bsondocument>(\"items\");\n\n items.UpdateOne(session1,\n Builders<bsondocument>.Filter.And(\n Builders<bsondocument>.Filter.Eq(\"sku\", \"111\"),\n Builders<bsondocument>.Filter.Eq(\"end\", BsonNull.Value)),\n Builders<bsondocument>.Update.Set(\"end\", currentDate));\n\n items.InsertOne(session1, new BsonDocument\n {\n {\"sku\", \"nuts-111\"},\n {\"name\", \"Pecans\"},\n {\"start\", currentDate}\n });\n}","programmingLanguage":"C#"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">using</span> (<span class="lg-highlight-keyword">var</span> session1 = client.StartSession(<span class="lg-highlight-keyword">new</span> ClientSessionOptions { CausalConsistency = <span class="lg-highlight-literal">true</span> }))</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> currentDate = DateTime.UtcNow.Date;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> items = client.GetDatabase(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">new</span> MongoDatabaseSettings</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ReadConcern = ReadConcern.Majority,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> WriteConcern = <span class="lg-highlight-keyword">new</span> WriteConcern(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> WriteConcern.WMode.Majority,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> TimeSpan.FromMilliseconds(<span class="lg-highlight-number">1000</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> })</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .GetCollection<BsonDocument>(<span class="lg-highlight-string">"items"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items.UpdateOne(session1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> Builders<BsonDocument>.Filter.And(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> Builders<BsonDocument>.Filter.Eq(<span class="lg-highlight-string">"sku"</span>, <span class="lg-highlight-string">"111"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> Builders<BsonDocument>.Filter.Eq(<span class="lg-highlight-string">"end"</span>, BsonNull.Value)),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> Builders<BsonDocument>.Update.Set(<span class="lg-highlight-string">"end"</span>, currentDate));</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items.InsertOne(session1, <span class="lg-highlight-keyword">new</span> BsonDocument</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"sku"</span>, <span class="lg-highlight-string">"nuts-111"</span>},</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"name"</span>, <span class="lg-highlight-string">"Pecans"</span>},</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"start"</span>, currentDate}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> });</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-24-menu" aria-describedby="select-24-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-25" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_n6tr7g1mn" class="leafygreen-ui-38lglc" id="_gxfd3csam" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// Example 1: Use a causally consistent session to ensure that the update occurs before the insert.\nClientSession session1 = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());\nDate currentDate = new Date();\nMongoCollection<document> items = client.getDatabase(\"test\")\n .withReadConcern(ReadConcern.MAJORITY)\n .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS))\n .getCollection(\"test\");\n\nitems.updateOne(session1, eq(\"sku\", \"111\"), set(\"end\", currentDate));\n\nDocument document = new Document(\"sku\", \"nuts-111\")\n .append(\"name\", \"Pecans\")\n .append(\"start\", currentDate);\nitems.insertOne(session1, document);","programmingLanguage":"Java"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Example 1:</span><span class="lg-highlight-comment"> Use a causally </span><span class="lg-highlight-comment">consistent</span><span class="lg-highlight-comment"> session to ensure </span><span class="lg-highlight-comment">that</span><span class="lg-highlight-comment"> the update occurs </span><span class="lg-highlight-comment">before the insert.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">ClientSession session1 = client.startSession(ClientSessionOptions.builder().causallyConsistent(<span class="lg-highlight-literal">true</span>).build());</td></tr><tr class=""><td class="leafygreen-ui-7razhx">Date currentDate = <span class="lg-highlight-keyword">new</span> Date();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">MongoCollection<Document> items = client.getDatabase(<span class="lg-highlight-string">"test"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .withReadConcern(ReadConcern.MAJORITY)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(<span class="lg-highlight-number">1000</span>, TimeUnit.MILLISECONDS))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .getCollection(<span class="lg-highlight-string">"test"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">items.updateOne(session1, eq(<span class="lg-highlight-string">"sku"</span>, <span class="lg-highlight-string">"111"</span>), <span class="lg-highlight-keyword">set</span>(<span class="lg-highlight-string">"end"</span>, currentDate));</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">Document document = <span class="lg-highlight-keyword">new</span> Document(<span class="lg-highlight-string">"sku"</span>, <span class="lg-highlight-string">"nuts-111"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .append(<span class="lg-highlight-string">"name"</span>, <span class="lg-highlight-string">"Pecans"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .append(<span class="lg-highlight-string">"start"</span>, currentDate);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">items.insertOne(session1, document);</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-27-menu" aria-describedby="select-27-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-28" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_seva8qn07" class="leafygreen-ui-38lglc" id="_3k2p9hs0y" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" async with await client.start_session(causal_consistency=True) as s1:\n current_date = datetime.datetime.today()\n items = client.get_database(\n \"test\",\n read_concern=ReadConcern(\"majority\"),\n write_concern=WriteConcern(\"majority\", wtimeout=1000),\n ).items\n await items.update_one(\n {\"sku\": \"111\", \"end\": None}, {\"$set\": {\"end\": current_date}}, session=s1\n )\n await items.insert_one(\n {\"sku\": \"nuts-111\", \"name\": \"Pecans\", \"start\": current_date}, session=s1\n )","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">async</span> <span class="lg-highlight-keyword">with</span> <span class="lg-highlight-keyword">await</span> client.start_session(causal_consistency=True) <span class="lg-highlight-keyword">as</span> s1:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> current_date = datetime.datetime.today()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items = client.get_database(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_concern=ReadConcern(<span class="lg-highlight-string">"majority"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> write_concern=WriteConcern(<span class="lg-highlight-string">"majority"</span>, wtimeout=<span class="lg-highlight-number">1000</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ).items</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">await</span> items.update_one(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"sku"</span>: <span class="lg-highlight-string">"111"</span>, <span class="lg-highlight-string">"end"</span>: None}, {<span class="lg-highlight-string">"$set"</span>: {<span class="lg-highlight-string">"end"</span>: current_date}}, session=s1</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">await</span> items.insert_one(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"sku"</span>: <span class="lg-highlight-string">"nuts-111"</span>, <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"Pecans"</span>, <span class="lg-highlight-string">"start"</span>: current_date}, session=s1</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-30-menu" aria-describedby="select-30-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-31" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_idnp5dks3" class="leafygreen-ui-38lglc" id="_zqx28zb7g" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\nmy $s1 = $conn->start_session({ causalConsistency => 1 });\n$items = $conn->get_database(\n \"test\", {\n read_concern => { level => 'majority' },\n write_concern => { w => 'majority', wtimeout => 10000 },\n }\n)->get_collection(\"items\");\n$items->update_one(\n {\n sku => 111,\n end => undef\n },\n {\n '$set' => { end => $current_date}\n },\n {\n session => $s1\n }\n);\n$items->insert_one(\n {\n sku => \"nuts-111\",\n name => \"Pecans\",\n start => $current_date\n },\n {\n session => $s1\n }\n);\n","programmingLanguage":"Perl"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><!-- -->my $s1 = $conn->start_session({ causalConsistency => <span class="lg-highlight-number">1</span> });<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$items = $conn->get_database(<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test"</span>, {<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_concern => { level => <span class="lg-highlight-string">'majority'</span> },<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> write_concern => { w => <span class="lg-highlight-string">'majority'</span>, wtimeout => <!-- --><!-- --><span class="lg-highlight-number">10000</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">)->get_collection(<!-- --><!-- --><span class="lg-highlight-string">"items"</span>);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$items->update_one(<!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> sku => <span class="lg-highlight-number">111</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> end => undef</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'$set'</span> => { end => $current_date}<!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session => $s1<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$items->insert_one(<!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> sku => <span class="lg-highlight-string">"nuts-111"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> name => <span class="lg-highlight-string">"Pecans"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> start => $current_date<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session => $s1<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);<!-- --></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-33-menu" aria-describedby="select-33-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-34" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_m6wvylxx5" class="leafygreen-ui-38lglc" id="_3pfruj548" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"$items = $client->selectDatabase(\n 'test',\n [\n 'readConcern' => new \\MongoDB\\Driver\\ReadConcern(\\MongoDB\\Driver\\ReadConcern::MAJORITY),\n 'writeConcern' => new \\MongoDB\\Driver\\WriteConcern(\\MongoDB\\Driver\\WriteConcern::MAJORITY, 1000),\n ],\n)->items;\n\n$s1 = $client->startSession(\n ['causalConsistency' => true],\n);\n\n$currentDate = new \\MongoDB\\BSON\\UTCDateTime();\n\n$items->updateOne(\n ['sku' => '111', 'end' => ['$exists' => false]],\n ['$set' => ['end' => $currentDate]],\n ['session' => $s1],\n);\n$items->insertOne(\n ['sku' => '111-nuts', 'name' => 'Pecans', 'start' => $currentDate],\n ['session' => $s1],\n);","programmingLanguage":"PHP"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">$items = $client->selectDatabase(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'test'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'readConcern'</span> => <span class="lg-highlight-keyword">new</span> \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::MAJORITY),<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'writeConcern'</span> => <span class="lg-highlight-keyword">new</span> \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, <span class="lg-highlight-number">1000</span>),<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)->items;<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$s1 = $client->startSession(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'causalConsistency'</span> => <span class="lg-highlight-literal">true</span>],</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><!-- -->$currentDate = <span class="lg-highlight-keyword">new</span> \MongoDB\BSON\UTCDateTime();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$items->updateOne(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'sku'</span> => <span class="lg-highlight-string">'111'</span>, <span class="lg-highlight-string">'end'</span> => [<span class="lg-highlight-string">'$exists'</span> => <span class="lg-highlight-literal">false</span>]],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'$set'</span> => [<span class="lg-highlight-string">'end'</span> => $currentDate]],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'session'</span> => $s1],</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$items->insertOne(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'sku'</span> => <span class="lg-highlight-string">'111-nuts'</span>, <span class="lg-highlight-string">'name'</span> => <span class="lg-highlight-string">'Pecans'</span>, <span class="lg-highlight-string">'start'</span> => $currentDate],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'session'</span> => $s1],</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);<!-- --></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-36-menu" aria-describedby="select-36-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-37" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_ss366qkrz" class="leafygreen-ui-38lglc" id="_unyu5e3mn" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"with client.start_session(causal_consistency=True) as s1:\n current_date = datetime.datetime.today()\n items = client.get_database(\n \"test\",\n read_concern=ReadConcern(\"majority\"),\n write_concern=WriteConcern(\"majority\", wtimeout=1000),\n ).items\n items.update_one(\n {\"sku\": \"111\", \"end\": None}, {\"$set\": {\"end\": current_date}}, session=s1\n )\n items.insert_one(\n {\"sku\": \"nuts-111\", \"name\": \"Pecans\", \"start\": current_date}, session=s1\n )","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">with</span> client.start_session(causal_consistency=True) <span class="lg-highlight-keyword">as</span> s1:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> current_date = datetime.datetime.today()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items = client.get_database(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_concern=ReadConcern(<span class="lg-highlight-string">"majority"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> write_concern=WriteConcern(<span class="lg-highlight-string">"majority"</span>, wtimeout=<span class="lg-highlight-number">1000</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ).items</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items.update_one(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"sku"</span>: <span class="lg-highlight-string">"111"</span>, <span class="lg-highlight-string">"end"</span>: None}, {<span class="lg-highlight-string">"$set"</span>: {<span class="lg-highlight-string">"end"</span>: current_date}}, session=s1</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items.insert_one(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"sku"</span>: <span class="lg-highlight-string">"nuts-111"</span>, <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"Pecans"</span>, <span class="lg-highlight-string">"start"</span>: current_date}, session=s1</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-39-menu" aria-describedby="select-39-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-40" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_pqwkwbm8b" class="leafygreen-ui-38lglc" id="_a3i7upi1a" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true))\nlet currentDate = Date()\nvar dbOptions = MongoDatabaseOptions(\n readConcern: .majority,\n writeConcern: try .majority(wtimeoutMS: 1000)\n)\nlet items = client1.db(\"test\", options: dbOptions).collection(\"items\")\nlet result1 = items.updateOne(\n filter: [\"sku\": \"111\", \"end\": .null],\n update: [\"$set\": [\"end\": .datetime(currentDate)]],\n session: s1\n).flatMap { _ in\n items.insertOne([\"sku\": \"nuts-111\", \"name\": \"Pecans\", \"start\": .datetime(currentDate)], session: s1)\n}","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: <span class="lg-highlight-literal">true</span>))<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> currentDate = Date()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> dbOptions = MongoDatabaseOptions(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> readConcern: .majority,<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> writeConcern: <!-- --><!-- --><span class="lg-highlight-keyword">try</span> .majority(wtimeoutMS: <span class="lg-highlight-number">1000</span>)<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> items = client1.db(<span class="lg-highlight-string">"test"</span>, options: dbOptions).collection(<!-- --><!-- --><!-- --><!-- --><!-- --><span class="lg-highlight-string">"items"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> result1 = items.updateOne(<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> filter: [<!-- --><!-- --><span class="lg-highlight-string">"sku"</span>: <span class="lg-highlight-string">"111"</span>, <span class="lg-highlight-string">"end"</span>: .<span class="lg-highlight-literal">null</span>],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> update: [<!-- --><!-- --><span class="lg-highlight-string">"$set"</span>: [<span class="lg-highlight-string">"end"</span>: .datetime(currentDate)]],<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session: s1<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">).flatMap { _ <!-- --><span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items.insertOne([<!-- --><!-- --><span class="lg-highlight-string">"sku"</span>: <!-- --><!-- --><!-- --><!-- --><!-- --><span class="lg-highlight-string">"nuts-111"</span>, <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"Pecans"</span>, <span class="lg-highlight-string">"start"</span>: .datetime(currentDate)], session: s1)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-42-menu" aria-describedby="select-42-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-43" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_yuxdgzxvq" class="leafygreen-ui-38lglc" id="_188zsq5gl" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true))\nlet currentDate = Date()\nvar dbOptions = MongoDatabaseOptions(\n readConcern: .majority,\n writeConcern: try .majority(wtimeoutMS: 1000)\n)\nlet items = client1.db(\"test\", options: dbOptions).collection(\"items\")\ntry items.updateOne(\n filter: [\"sku\": \"111\", \"end\": .null],\n update: [\"$set\": [\"end\": .datetime(currentDate)]],\n session: s1\n)\ntry items.insertOne([\"sku\": \"nuts-111\", \"name\": \"Pecans\", \"start\": .datetime(currentDate)], session: s1)","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: <span class="lg-highlight-literal">true</span>))<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> currentDate = Date()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> dbOptions = MongoDatabaseOptions(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> readConcern: .majority,<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> writeConcern: <!-- --><!-- --><span class="lg-highlight-keyword">try</span> .majority(wtimeoutMS: <span class="lg-highlight-number">1000</span>)<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> items = client1.db(<span class="lg-highlight-string">"test"</span>, options: dbOptions).collection(<!-- --><!-- --><!-- --><!-- --><!-- --><span class="lg-highlight-string">"items"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">try</span> items.updateOne(<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> filter: [<!-- --><!-- --><span class="lg-highlight-string">"sku"</span>: <span class="lg-highlight-string">"111"</span>, <span class="lg-highlight-string">"end"</span>: .<span class="lg-highlight-literal">null</span>],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> update: [<!-- --><!-- --><span class="lg-highlight-string">"$set"</span>: [<span class="lg-highlight-string">"end"</span>: .datetime(currentDate)]],<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session: s1<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">try</span> items.insertOne([<!-- --><!-- --><span class="lg-highlight-string">"sku"</span>: <!-- --><!-- --><!-- --><!-- --><!-- --><span class="lg-highlight-string">"nuts-111"</span>, <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"Pecans"</span>, <span class="lg-highlight-string">"start"</span>: .datetime(currentDate)], session: s1)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-45-menu" aria-describedby="select-45-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-46" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If another client needs to read all current
<code class="leafygreen-ui-1l06pbn">
sku
</code>
values, you can
advance the cluster time and the operation time to that of the other
session to ensure that this client is causally consistent with the
other session and read after the two writes:
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-f2qztv" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-19">
<div aria-label="Tabs to describe usage of drivers" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_kjmyojuy7" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_kb2fsomaz" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
C
</div>
</button>
<button aria-controls="_vyf2pfd50" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_0qwrghxsb" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C#
</div>
</button>
<button aria-controls="_dcp332w52" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_wm8lphqhy" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Java (Sync)
</div>
</button>
<button aria-controls="_9hee168lq" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_73t0vqvsq" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Motor
</div>
</button>
<button aria-controls="_i0i4nqatq" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_zyp9qhwzx" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Perl
</div>
</button>
<button aria-controls="_q59kkjt2h" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_5k4kiiybm" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
PHP
</div>
</button>
<button aria-controls="_fmivixa1b" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_r4jogr5kh" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Python
</div>
</button>
<button aria-controls="_x2kw5ivqe" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_d15lq3f4w" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Async)
</div>
</button>
<button aria-controls="_h8zuxcrr7" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_p1o8u5y9t" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Sync)
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_qg27wyh73" class="" id="_kcu7idopd" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\n /* Make a new session, session2, and make it causally-consistent\n * with session1, so that session2 will read session1's writes. */\n session2 = mongoc_client_start_session (client, session_opts, &error);\n if (!session2) {\n fprintf (stderr, \"couldn't start session: %s\\n\", error.message);\n goto cleanup;\n }\n\n /* Set the cluster time for session2 to session1's cluster time */\n cluster_time = mongoc_client_session_get_cluster_time (session1);\n mongoc_client_session_advance_cluster_time (session2, cluster_time);\n\n /* Set the operation time for session2 to session2's operation time */\n mongoc_client_session_get_operation_time (session1, ×tamp, &increment);\n mongoc_client_session_advance_operation_time (session2,\n\t\t\t\t timestamp,\n\t\t\t\t increment);\n\n /* Run a find on session2, which should now find all writes done\n * inside of session1 */\n find_opts = bson_new ();\n res = mongoc_client_session_append (session2, find_opts, &error);\n if (!res) {\n fprintf (stderr, \"couldn't add session to opts: %s\\n\", error.message);\n goto cleanup;\n }\n\n find_query = BCON_NEW (\"end\", BCON_NULL);\n read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);\n cursor = mongoc_collection_find_with_opts (coll,\n\t\t\t query,\n\t\t\t find_opts,\n\t\t\t read_prefs);\n\n while (mongoc_cursor_next (cursor, &result)) {\n json = bson_as_relaxed_extended_json (result, NULL);\n fprintf (stdout, \"Document: %s\\n\", json);\n bson_free (json);\n }\n\n if (mongoc_cursor_error (cursor, &error)) {\n fprintf (stderr, \"cursor failure: %s\\n\", error.message);\n goto cleanup;\n }\n","programmingLanguage":"C"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">/*</span><span class="lg-highlight-comment"> Make a new </span><span class="lg-highlight-comment">session, session2,</span><span class="lg-highlight-comment"> and make it </span><span class="lg-highlight-comment">causally-consistent</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"> * with session1, so that session2 will read session1's writes. */</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session2 = mongoc_client_start_session (client, session_opts, &error);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> (!session2) {<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stderr, <span class="lg-highlight-string">"couldn't start session: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">goto</span> cleanup;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">/*</span><span class="lg-highlight-comment"> Set the cluster </span><span class="lg-highlight-comment">time for session2 to session1's cluster time */</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cluster_time = mongoc_client_session_get_cluster_time (session1);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_client_session_advance_cluster_time (session2, cluster_time);<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">/*</span><span class="lg-highlight-comment"> Set the operation </span><span class="lg-highlight-comment">time for session2 to session2's operation time */</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_client_session_get_operation_time (session1, &timestamp, &increment);<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_client_session_advance_operation_time (session2,<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> timestamp,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> increment);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">/*</span><span class="lg-highlight-comment"> Run a find </span><span class="lg-highlight-comment">on session2,</span><span class="lg-highlight-comment"> which should now </span><span class="lg-highlight-comment">find all writes done</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"> * inside of session1 */</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> find_opts = bson_new ();<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> res = mongoc_client_session_append (session2, find_opts, &error);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> (!res) {<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stderr, <span class="lg-highlight-string">"couldn't add session to opts: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">goto</span> cleanup;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> find_query = BCON_NEW (<!-- --><!-- --><span class="lg-highlight-string">"end"</span>, BCON_NULL);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor = mongoc_collection_find_with_opts (coll,<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> query,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> find_opts,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_prefs);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">while</span> (mongoc_cursor_next (cursor, &result)) {<!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> json = bson_as_relaxed_extended_json (result, NULL);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stdout, <span class="lg-highlight-string">"Document: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, json);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> bson_free (json);<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> (mongoc_cursor_error (cursor, &error)) {<!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> fprintf (stderr, <span class="lg-highlight-string">"cursor failure: %s</span><!-- --><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">goto</span> cleanup;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-48-menu" aria-describedby="select-48-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-49" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_bwlvydgd4" class="leafygreen-ui-38lglc" id="_60cinc105" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"using (var session2 = client.StartSession(new ClientSessionOptions { CausalConsistency = true }))\n{\n session2.AdvanceClusterTime(session1.ClusterTime);\n session2.AdvanceOperationTime(session1.OperationTime);\n\n var items = client.GetDatabase(\n \"test\",\n new MongoDatabaseSettings\n {\n ReadPreference = ReadPreference.Secondary,\n ReadConcern = ReadConcern.Majority,\n WriteConcern = new WriteConcern(WriteConcern.WMode.Majority, TimeSpan.FromMilliseconds(1000))\n })\n .GetCollection<bsondocument>(\"items\");\n\n var filter = Builders<bsondocument>.Filter.Eq(\"end\", BsonNull.Value);\n foreach (var item in items.Find(session2, filter).ToEnumerable())\n {\n // process item\n }\n}","programmingLanguage":"C#"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">using</span> (<span class="lg-highlight-keyword">var</span> session2 = client.StartSession(<span class="lg-highlight-keyword">new</span> ClientSessionOptions { CausalConsistency = <span class="lg-highlight-literal">true</span> }))</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session2.AdvanceClusterTime(session1.ClusterTime);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> session2.AdvanceOperationTime(session1.OperationTime);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> items = client.GetDatabase(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">new</span> MongoDatabaseSettings</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ReadPreference = ReadPreference.Secondary,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ReadConcern = ReadConcern.Majority,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> WriteConcern = <span class="lg-highlight-keyword">new</span> WriteConcern(WriteConcern.WMode.Majority, TimeSpan.FromMilliseconds(<span class="lg-highlight-number">1000</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> })</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .GetCollection<BsonDocument>(<span class="lg-highlight-string">"items"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> filter = Builders<BsonDocument>.Filter.Eq(<span class="lg-highlight-string">"end"</span>, BsonNull.Value);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">foreach</span> (<span class="lg-highlight-keyword">var</span> item <span class="lg-highlight-keyword">in</span> items.Find(session2, filter).ToEnumerable())</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// process item</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-51-menu" aria-describedby="select-51-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-52" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_5nj4uwavv" class="leafygreen-ui-38lglc" id="_sdmf0wsrh" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// Example 2: Advance the cluster time and the operation time to that of the other session to ensure that\n// this client is causally consistent with the other session and read after the two writes.\nClientSession session2 = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());\nsession2.advanceClusterTime(session1.getClusterTime());\nsession2.advanceOperationTime(session1.getOperationTime());\n\nitems = client.getDatabase(\"test\")\n .withReadPreference(ReadPreference.secondary())\n .withReadConcern(ReadConcern.MAJORITY)\n .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS))\n .getCollection(\"items\");\n\nfor (Document item: items.find(session2, eq(\"end\", BsonNull.VALUE))) {\n System.out.println(item);\n}","programmingLanguage":"Java"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Example 2:</span><span class="lg-highlight-comment"> Advance the cluster </span><span class="lg-highlight-comment">time</span><span class="lg-highlight-comment"> and the operation </span><span class="lg-highlight-comment">time to that of</span><span class="lg-highlight-comment"> the other session </span><span class="lg-highlight-comment">to ensure that</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> this client is </span><span class="lg-highlight-comment">causally</span><span class="lg-highlight-comment"> consistent with the </span><span class="lg-highlight-comment">other</span><span class="lg-highlight-comment"> session and read </span><span class="lg-highlight-comment">after the two writes.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">ClientSession session2 = client.startSession(ClientSessionOptions.builder().causallyConsistent(<span class="lg-highlight-literal">true</span>).build());</td></tr><tr class=""><td class="leafygreen-ui-7razhx">session2.advanceClusterTime(session1.getClusterTime());</td></tr><tr class=""><td class="leafygreen-ui-7razhx">session2.advanceOperationTime(session1.getOperationTime());</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">items = client.getDatabase(<span class="lg-highlight-string">"test"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .withReadPreference(ReadPreference.secondary())</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .withReadConcern(ReadConcern.MAJORITY)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(<span class="lg-highlight-number">1000</span>, TimeUnit.MILLISECONDS))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .getCollection(<span class="lg-highlight-string">"items"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">for</span> (Document item: items.find(session2, eq(<span class="lg-highlight-string">"end"</span>, BsonNull.VALUE))) {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> System.<span class="lg-highlight-keyword">out</span>.println(item);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-54-menu" aria-describedby="select-54-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-55" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_u6d9pig4a" class="leafygreen-ui-38lglc" id="_m3lin9ua6" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" async with await client.start_session(causal_consistency=True) as s2:\n s2.advance_cluster_time(s1.cluster_time)\n s2.advance_operation_time(s1.operation_time)\n\n items = client.get_database(\n \"test\",\n read_preference=ReadPreference.SECONDARY,\n read_concern=ReadConcern(\"majority\"),\n write_concern=WriteConcern(\"majority\", wtimeout=1000),\n ).items\n async for item in items.find({\"end\": None}, session=s2):\n print(item)","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">async</span> <span class="lg-highlight-keyword">with</span> <span class="lg-highlight-keyword">await</span> client.start_session(causal_consistency=True) <span class="lg-highlight-keyword">as</span> s2:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> s2.advance_cluster_time(s1.cluster_time)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> s2.advance_operation_time(s1.operation_time)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items = client.get_database(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_preference=ReadPreference.SECONDARY,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_concern=ReadConcern(<span class="lg-highlight-string">"majority"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> write_concern=WriteConcern(<span class="lg-highlight-string">"majority"</span>, wtimeout=<span class="lg-highlight-number">1000</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ).items</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">async</span> <span class="lg-highlight-keyword">for</span> item <span class="lg-highlight-keyword">in</span> items.find({<span class="lg-highlight-string">"end"</span>: None}, session=s2):</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> print(item)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-57-menu" aria-describedby="select-57-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-58" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_yi3xu3g9b" class="leafygreen-ui-38lglc" id="_d2t478d9m" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\nmy $s2 = $conn->start_session({ causalConsistency => 1 });\n$s2->advance_cluster_time( $s1->cluster_time );\n$s2->advance_operation_time( $s1->operation_time );\n\n$items = $conn->get_database(\n \"test\", {\n read_preference => 'secondary',\n read_concern => { level => 'majority' },\n write_concern => { w => 'majority', wtimeout => 10000 },\n }\n)->get_collection(\"items\");\n$cursor = $items->find( { end => undef }, { session => $s2 } );\n\nfor my $item ( $cursor->all ) {\n say join(\" \", %$item);\n}\n","programmingLanguage":"Perl"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><!-- -->my $s2 = $conn->start_session({ causalConsistency => <span class="lg-highlight-number">1</span> });<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$s2->advance_cluster_time( $s1->cluster_time );<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$s2->advance_operation_time( $s1->operation_time );<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$items = $conn->get_database(<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test"</span>, {<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_preference => <span class="lg-highlight-string">'secondary'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_concern => { level => <span class="lg-highlight-string">'majority'</span> },<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> write_concern => { w => <span class="lg-highlight-string">'majority'</span>, wtimeout => <!-- --><!-- --><span class="lg-highlight-number">10000</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">)->get_collection(<!-- --><!-- --><span class="lg-highlight-string">"items"</span>);<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$cursor = $items->find( { end => undef }, { session => $s2 } );<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">for</span> my $item ( $cursor->all ) {<!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-function">say </span><span class="lg-highlight-title lg-highlight-function">join</span><span class="lg-highlight-function">(</span><span class="lg-highlight-string lg-highlight-params lg-highlight-function">" "</span><span class="lg-highlight-params lg-highlight-function">, %$item</span><!-- --><span class="lg-highlight-function">)</span>;<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-60-menu" aria-describedby="select-60-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-61" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_4vhpitb3q" class="leafygreen-ui-38lglc" id="_tairu5oxw" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"$s2 = $client->startSession(\n ['causalConsistency' => true],\n);\n$s2->advanceClusterTime($s1->getClusterTime());\n$s2->advanceOperationTime($s1->getOperationTime());\n\n$items = $client->selectDatabase(\n 'test',\n [\n 'readPreference' => new \\MongoDB\\Driver\\ReadPreference(\\MongoDB\\Driver\\ReadPreference::SECONDARY),\n 'readConcern' => new \\MongoDB\\Driver\\ReadConcern(\\MongoDB\\Driver\\ReadConcern::MAJORITY),\n 'writeConcern' => new \\MongoDB\\Driver\\WriteConcern(\\MongoDB\\Driver\\WriteConcern::MAJORITY, 1000),\n ],\n)->items;\n\n$result = $items->find(\n ['end' => ['$exists' => false]],\n ['session' => $s2],\n);\nforeach ($result as $item) {\n var_dump($item);\n}\n","programmingLanguage":"PHP"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">$s2 = $client->startSession(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'causalConsistency'</span> => <span class="lg-highlight-literal">true</span>],</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><!-- --><!-- -->$s2->advanceClusterTime($s1->getClusterTime());</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><!-- --><!-- -->$s2->advanceOperationTime($s1->getOperationTime());</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$items = $client->selectDatabase(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'test'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'readPreference'</span> => <span class="lg-highlight-keyword">new</span> \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::SECONDARY),<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'readConcern'</span> => <span class="lg-highlight-keyword">new</span> \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::MAJORITY),<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'writeConcern'</span> => <span class="lg-highlight-keyword">new</span> \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, <span class="lg-highlight-number">1000</span>),<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)->items;<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$result = $items->find(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'end'</span> => [<span class="lg-highlight-string">'$exists'</span> => <span class="lg-highlight-literal">false</span>]],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'session'</span> => $s2],</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">foreach</span> ($result <span class="lg-highlight-keyword">as</span> $item) {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> var_dump($item);<!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-63-menu" aria-describedby="select-63-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-64" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_5ft7et9bt" class="leafygreen-ui-38lglc" id="_9fbkk377y" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"with client.start_session(causal_consistency=True) as s2:\n s2.advance_cluster_time(s1.cluster_time)\n s2.advance_operation_time(s1.operation_time)\n\n items = client.get_database(\n \"test\",\n read_preference=ReadPreference.SECONDARY,\n read_concern=ReadConcern(\"majority\"),\n write_concern=WriteConcern(\"majority\", wtimeout=1000),\n ).items\n for item in items.find({\"end\": None}, session=s2):\n print(item)","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">with</span> client.start_session(causal_consistency=True) <span class="lg-highlight-keyword">as</span> s2:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> s2.advance_cluster_time(s1.cluster_time)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> s2.advance_operation_time(s1.operation_time)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> items = client.get_database(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_preference=ReadPreference.SECONDARY,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> read_concern=ReadConcern(<span class="lg-highlight-string">"majority"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> write_concern=WriteConcern(<span class="lg-highlight-string">"majority"</span>, wtimeout=<span class="lg-highlight-number">1000</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ).items</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">for</span> item <span class="lg-highlight-keyword">in</span> items.find({<span class="lg-highlight-string">"end"</span>: None}, session=s2):</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> print(item)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-66-menu" aria-describedby="select-66-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-67" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_rbae97s0n" class="leafygreen-ui-38lglc" id="_0hc4nk81y" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let options = ClientSessionOptions(causalConsistency: true)\nlet result2: EventLoopFuture<void> = client2.withSession(options: options) { s2 in\n // The cluster and operation times are guaranteed to be non-nil since we already used s1 for operations above.\n s2.advanceClusterTime(to: s1.clusterTime!)\n s2.advanceOperationTime(to: s1.operationTime!)\n\n dbOptions.readPreference = .secondary\n let items2 = client2.db(\"test\", options: dbOptions).collection(\"items\")\n\n return items2.find([\"end\": .null], session: s2).flatMap { cursor in\n cursor.forEach { item in\n print(item)\n }\n }\n}","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> options = ClientSessionOptions(causalConsistency: <span class="lg-highlight-literal">true</span>)<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> result2: EventLoopFuture<Void> = client2.withSession(options: options) { s2 <span class="lg-highlight-keyword">in</span><!-- --><!-- --><!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> The cluster and </span><span class="lg-highlight-comment">operation</span><span class="lg-highlight-comment"> times are guaranteed </span><span class="lg-highlight-comment">to be non-nil since we already used s1 for operations above.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> s2.advanceClusterTime(to: s1.clusterTime!)<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> s2.advanceOperationTime(to: s1.operationTime!)<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> dbOptions.readPreference = .secondary</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> items2 = client2.db(<span class="lg-highlight-string">"test"</span>, options: dbOptions).collection(<!-- --><!-- --><!-- --><!-- --><!-- --><span class="lg-highlight-string">"items"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> items2.find([<!-- --><!-- --><span class="lg-highlight-string">"end"</span>: .<span class="lg-highlight-literal">null</span>], session: s2).flatMap { cursor <!-- --><!-- --><span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor.forEach { <!-- --><!-- --><span class="lg-highlight-function">item </span><span class="lg-highlight-keyword lg-highlight-function">in</span><span class="lg-highlight-function"></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-function"> </span><span class="lg-highlight-title lg-highlight-function">print</span><span class="lg-highlight-function">(</span><span class="lg-highlight-params lg-highlight-function">item</span><span class="lg-highlight-function">)</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-function"> }</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-function"> }</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-function">}</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-69-menu" aria-describedby="select-69-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-70" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_z2y3pjaem" class="leafygreen-ui-38lglc" id="_85az0yv2d" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"try client2.withSession(options: ClientSessionOptions(causalConsistency: true)) { s2 in\n // The cluster and operation times are guaranteed to be non-nil since we already used s1 for operations above.\n s2.advanceClusterTime(to: s1.clusterTime!)\n s2.advanceOperationTime(to: s1.operationTime!)\n\n dbOptions.readPreference = .secondary\n let items2 = client2.db(\"test\", options: dbOptions).collection(\"items\")\n for item in try items2.find([\"end\": .null], session: s2) {\n print(item)\n }\n}","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light cs leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">try</span> client2.withSession(options: ClientSessionOptions(causalConsistency: <!-- --><!-- --><span class="lg-highlight-literal">true</span>)) { s2 <!-- --><!-- --><!-- --><!-- --><!-- --><span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> The cluster and </span><span class="lg-highlight-comment">operation</span><span class="lg-highlight-comment"> times are guaranteed </span><span class="lg-highlight-comment">to be non-nil since we already used s1 for operations above.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> s2.advanceClusterTime(to: s1.clusterTime!)<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> s2.advanceOperationTime(to: s1.operationTime!)<!-- --><!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> dbOptions.readPreference = .secondary</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> items2 = client2.db(<span class="lg-highlight-string">"test"</span>, options: dbOptions).collection(<!-- --><!-- --><!-- --><!-- --><!-- --><span class="lg-highlight-string">"items"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">for</span> item <span class="lg-highlight-keyword">in</span> <span class="lg-highlight-keyword">try</span> items2.find([<!-- --><!-- --><span class="lg-highlight-string">"end"</span>: .<span class="lg-highlight-literal">null</span>], session: s2) {<!-- --><!-- --></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> print(item)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-72-menu" aria-describedby="select-72-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-73" type="button" value="C">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="29" role="presentation" viewbox="0 0 28 29" width="28" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
C
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-causal-consistency-limitations">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Limitations
<a class="headerlink leafygreen-ui-1kepx47" href="#limitations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="limitations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following operations that build in-memory structures are not
causally consistent:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Operation
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Notes
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-dbcommand-dbcmd.collStats">
<code class="leafygreen-ui-1l06pbn">
collStats
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/collStats/#mongodb-pipeline-pipe.-collStats">
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
</a>
with
<code class="leafygreen-ui-1l06pbn">
latencyStats
</code>
option.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/currentOp/#mongodb-pipeline-pipe.-currentOp">
<code class="leafygreen-ui-1l06pbn">
$currentOp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns an error if the operation is associated with a causally
consistent client session.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dbHash/#mongodb-dbcommand-dbcmd.dbHash">
<code class="leafygreen-ui-1l06pbn">
dbHash
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dbStats/#mongodb-dbcommand-dbcmd.dbStats">
<code class="leafygreen-ui-1l06pbn">
dbStats
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns an error if the operation is associated with a causally
consistent client session.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/indexStats/#mongodb-pipeline-pipe.-indexStats">
<code class="leafygreen-ui-1l06pbn">
$indexStats
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
<code class="leafygreen-ui-1l06pbn">
mapReduce
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/ping/#mongodb-dbcommand-dbcmd.ping">
<code class="leafygreen-ui-1l06pbn">
ping
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns an error if the operation is associated with a causally
consistent client session.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-dbcommand-dbcmd.serverStatus">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns an error if the operation is associated with a causally
consistent client session.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/validate/#mongodb-dbcommand-dbcmd.validate">
<code class="leafygreen-ui-1l06pbn">
validate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/rs.printSecondaryReplicationInfo/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
rs.printSecondaryReplicationInfo()
<a class="headerlink leafygreen-ui-1kepx47" href="#rs.printsecondaryreplicationinfo--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="rs.printsecondaryreplicationinfo--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output" target="_self">
<span>
Output
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
rs.printSecondaryReplicationInfo()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-rs.printSecondaryReplicationInfo" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-rs.printSecondaryReplicationInfo">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Prints a formatted report of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
</a>
status from the
perspective of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
member of the set. The output is
identical to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.printSecondaryReplicationInfo/#mongodb-method-db.printSecondaryReplicationInfo">
<code class="leafygreen-ui-1l06pbn">
db.printSecondaryReplicationInfo()
</code>
.
</a>
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Output
<a class="headerlink leafygreen-ui-1kepx47" href="#output" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="output">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Example
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printSecondaryReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printSecondaryReplicationInfo()
</code>
</span>
</a>
output when run on a replica set with two secondary
members:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"source: m1.example.net:27002\n syncedTo: Mon Mar 01 2021 16:30:50 GMT-0800 (PST)\n 0 secs (0 hrs) behind the primary\nsource: m2.example.net:27003\n syncedTo: Mon Mar 01 2021 16:30:50 GMT-0800 (PST)\n 0 secs (0 hrs) behind the primary","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">source</span>: <!-- -->m1.<span class="lg-highlight-property">example</span>.<span class="lg-highlight-property">net</span>:<span class="lg-highlight-number">27002</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">syncedTo</span>: <span class="lg-highlight-title lg-highlight-class">Mon</span> <span class="lg-highlight-title lg-highlight-class">Mar</span> <span class="lg-highlight-number">01</span> <span class="lg-highlight-number">2021</span> <span class="lg-highlight-number">16</span>:<span class="lg-highlight-number">30</span>:<span class="lg-highlight-number">50</span> <span class="lg-highlight-variable lg-highlight-constant">GMT</span>-<span class="lg-highlight-number">0800</span> <!-- -->(<span class="lg-highlight-variable lg-highlight-constant">PST</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-number">0</span> secs <!-- -->(<span class="lg-highlight-number">0</span> hrs) behind the primary</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">source</span>: <!-- -->m2.<span class="lg-highlight-property">example</span>.<span class="lg-highlight-property">net</span>:<span class="lg-highlight-number">27003</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">syncedTo</span>: <span class="lg-highlight-title lg-highlight-class">Mon</span> <span class="lg-highlight-title lg-highlight-class">Mar</span> <span class="lg-highlight-number">01</span> <span class="lg-highlight-number">2021</span> <span class="lg-highlight-number">16</span>:<span class="lg-highlight-number">30</span>:<span class="lg-highlight-number">50</span> <span class="lg-highlight-variable lg-highlight-constant">GMT</span>-<span class="lg-highlight-number">0800</span> <!-- -->(<span class="lg-highlight-variable lg-highlight-constant">PST</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-number">0</span> secs <!-- -->(<span class="lg-highlight-number">0</span> hrs) behind the primary</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printSecondaryReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printSecondaryReplicationInfo()
</code>
</span>
</a>
method run in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
does
<strong>
not
</strong>
return
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-JSON">
JSON
</a>
. Use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printSecondaryReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printSecondaryReplicationInfo()
</code>
</span>
</a>
for manual inspection, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.status/#mongodb-method-rs.status">
<code class="leafygreen-ui-1l06pbn">
rs.status()
</code>
</a>
in scripts.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
A
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-delayed-member/#std-label-replica-set-delayed-members">
delayed member
</a>
may show as
<code class="leafygreen-ui-1l06pbn">
0
</code>
seconds behind the primary when the inactivity period on the primary is
greater than the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.secondaryDelaySecs">
<code class="leafygreen-ui-1l06pbn">
members[n].secondaryDelaySecs
</code>
</a>
value.
</p>
<p class="leafygreen-ui-1kp3ins">
A member may show a negative time value behind the primary when
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printSecondaryReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printSecondaryReplicationInfo()
</code>
</span>
</a>
is run. This is expected if
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printSecondaryReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printSecondaryReplicationInfo()
</code>
</span>
</a>
is run after a secondary replicates
a write that follows a period of inactivity, but before the secondary
receives a heartbeat from the primary with the latest
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-optime">
optime
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The lag reported by secondaries may not be representative of cluster
health. Negative values do not indicate that the secondary is ahead
of the primary.
</p>
<p class="leafygreen-ui-1kp3ins">
To obtain the most current status for your replica set, run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printSecondaryReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printSecondaryReplicationInfo()
</code>
</span>
</a>
on the primary.
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sh.disableAutoSplit/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.disableAutoSplit
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.disableautosplit" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sh.disableautosplit">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0.3, automatic chunk splitting is not performed.
This is because of balancing policy improvements. Auto-splitting commands
still exist, but do not perform an operation.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
sh.disableAutoSplit()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.disableAutoSplit" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-sh.disableAutoSplit">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Disables the autosplit flag in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.settings">
<code class="leafygreen-ui-1l06pbn">
config.settings
</code>
</a>
collection. When auto-splitting is enabled for a sharded cluster,
MongoDB automatically splits chunks based on the shard key values
the chunk represents to keep the chunks from growing too large.
</p>
<p class="leafygreen-ui-1kp3ins">
You can only run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.disableAutoSplit" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.disableAutoSplit()
</code>
</span>
</a>
from a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
session that is connected to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.disableAutoSplit" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.disableAutoSplit()
</code>
</span>
</a>
errors if run on a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0.3, automatic chunk splitting is not performed.
This is because of balancing policy improvements. Auto-splitting commands
still exist, but do not perform an operation.
</p>
<p class="leafygreen-ui-1kp3ins">
In MongoDB versions earlier than 6.0.3,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.stopBalancer/#mongodb-method-sh.stopBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.stopBalancer()
</code>
</a>
also disables auto-splitting for the sharded cluster.
</p>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/">
Manage Sharded Cluster Balancer
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing">
Sharded Cluster Balancer
</a>
</p>
</li>
</ul>
</div>
</div>
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.collection.explain/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.collection.explain()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.collection.explain--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="db.collection.explain--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output" target="_self">
<span>
Output
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-147elvs">
<p class="leafygreen-ui-1ronz74">
MongoDB with drivers
</p>
<div class="leafygreen-ui-18k5rfm">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. To see the equivalent
method in a MongoDB driver, see the corresponding page for your
programming language:
</p>
</div>
<div class="leafygreen-ui-zgrwqu notranslate">
<a class="leafygreen-ui-v22cck" href="https://mongodb.github.io/mongo-csharp-driver/2.29.0/api/MongoDB.Driver/MongoDB.Driver.Core.Operations.ExplainOperation.-ctor.html" target="_self">
<svg class="leafygreen-ui-x3zji3" fill="none" height="24" viewbox="0 0 28 29" width="24" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59964)">
<g clip-path="url(#clip1_1351_59964)">
<path d="M25.7709 9.2495C25.7709 8.79827 25.678 8.39408 25.4827 8.04627C25.2874 7.70785 25.0084 7.42586 24.627 7.20025C21.465 5.35781 18.2937 3.51538 15.1317 1.67295C14.2761 1.17474 13.4484 1.19354 12.6021 1.70115C11.3466 2.45316 5.04112 6.10042 3.15321 7.20025C2.3813 7.65142 2 8.34705 2 9.24005C2 12.9531 2 16.6568 2 20.3698C2 20.8117 2.093 21.2064 2.279 21.5449C2.4743 21.8927 2.7626 22.1841 3.15321 22.4191C5.03182 23.5189 11.3373 27.1662 12.6021 27.9182C13.4484 28.4258 14.2761 28.4446 15.1317 27.9463C18.2937 26.1039 21.465 24.2615 24.627 22.4191C25.0177 22.1935 25.306 21.9021 25.5012 21.5449C25.6873 21.2064 25.7803 20.8117 25.7803 20.3698C25.7709 20.3698 25.7709 12.9531 25.7709 9.2495Z" fill="#A179DC">
</path>
<path d="M13.9227 14.7673L2.27905 21.5449C2.47435 21.8927 2.76265 22.1841 3.15326 22.4191C5.03187 23.5189 11.3373 27.1662 12.6021 27.9182C13.4484 28.4258 14.2762 28.4445 15.1317 27.9463C18.2938 26.1039 21.4651 24.2615 24.6271 22.4191C25.0177 22.1935 25.306 21.9021 25.5013 21.5449L13.9227 14.7673Z" fill="#280068">
</path>
<path d="M25.7711 9.24949C25.7711 8.79827 25.6781 8.39408 25.4828 8.04626L13.9229 14.7768L25.4921 21.5543C25.6781 21.2159 25.7711 20.8211 25.7711 20.3793C25.7711 20.3699 25.7711 12.9531 25.7711 9.24949Z" fill="#390091">
</path>
<path d="M20.7771 12.2387V13.5078H22.0325V12.2387H22.6557V13.5078H23.9111V14.1376H22.6557V15.4066H23.9111V16.0364H22.6557V17.3054H22.0325V16.0364H20.7771V17.3054H20.1539V16.0364H18.8984V15.4066H20.1539V14.1376H18.8984V13.5078H20.1539V12.2387H20.7771ZM22.0325 14.1376H20.7771V15.4066H22.0325V14.1376Z" fill="white">
</path>
<path d="M13.9508 5.87482C17.2151 5.87482 20.0702 7.67022 21.6047 10.3399L21.5861 10.3117L17.7452 12.5489C16.9919 11.2517 15.5969 10.3775 14.0066 10.3587H13.9601C11.5328 10.3587 9.56116 12.3515 9.56116 14.805C9.56116 15.604 9.77503 16.3654 10.1377 17.014C10.9004 18.3488 12.3232 19.2512 13.9601 19.2512C15.6062 19.2512 17.0384 18.3394 17.7917 16.9858L17.7731 17.014L21.614 19.2606C20.0981 21.9021 17.2895 23.6975 14.0531 23.7257H13.9508C10.6679 23.7257 7.81274 21.9209 6.28753 19.2418C5.54353 17.9352 5.11572 16.4124 5.11572 14.7956C5.11572 9.87931 9.06825 5.87482 13.9508 5.87482Z" fill="white">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59964">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59964">
<rect fill="white" height="27" transform="translate(2 1.30966)" width="24">
</rect>
</clippath>
</defs>
</svg>
<span>
C#
</span>
</a>
<a class="leafygreen-ui-v22cck" href="https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/aggregation/#explain-aggregation-example" target="_self">
<svg class="leafygreen-ui-x3zji3" fill="none" height="24" viewbox="0 0 30 41" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M10.0921 30.7949C10.0921 30.7949 8.57608 31.6793 11.166 31.9636C14.2929 32.311 15.9037 32.2794 19.3464 31.6161C19.3464 31.6161 20.2623 32.1846 21.5257 32.6901C13.7876 35.9748 4.02791 32.5005 10.0921 30.7949Z" fill="#0074BD">
</path>
<path d="M9.14539 26.4678C9.14539 26.4678 7.43985 27.7312 10.0298 27.9838C13.3778 28.3312 15.9992 28.3628 20.579 27.4785C20.579 27.4785 21.2107 28.1101 22.1898 28.4576C12.8724 31.2054 2.44949 28.6787 9.14539 26.4678Z" fill="#0074BD">
</path>
<path d="M17.1039 19.1398C18.9989 21.3191 16.5985 23.3089 16.5985 23.3089C16.5985 23.3089 21.431 20.8138 19.2201 17.6869C17.1355 14.7811 15.5563 13.3282 24.1788 8.33789C24.1473 8.33789 10.6607 11.6858 17.1039 19.1398Z" fill="#EA2D2E">
</path>
<path d="M27.3377 33.9845C27.3377 33.9845 28.4431 34.9004 26.1059 35.6269C21.6525 36.985 7.53417 37.3957 3.61769 35.6901C2.19639 35.0899 4.84949 34.2372 5.67069 34.0477C6.52347 33.8582 7.02884 33.8898 7.02884 33.8898C5.44961 32.7843 -3.10981 36.0691 2.67016 36.9851C18.4308 39.5433 31.4121 35.8164 27.3377 33.9845Z" fill="#0074BD">
</path>
<path d="M10.8194 21.983C10.8194 21.983 3.6497 23.6885 8.2926 24.3203C10.2509 24.5729 14.1357 24.5097 17.7995 24.2255C20.7685 23.9728 23.7375 23.4359 23.7375 23.4359C23.7375 23.4359 22.6951 23.8781 21.9372 24.415C14.6411 26.3416 0.585992 25.4257 4.62881 23.4675C8.00839 21.7935 10.8194 21.983 10.8194 21.983Z" fill="#0074BD">
</path>
<path d="M23.6745 29.1846C31.0969 25.3313 27.6542 21.6359 25.2538 22.1413C24.6537 22.2676 24.401 22.3623 24.401 22.3623C24.401 22.3623 24.6221 22.0149 25.0327 21.8886C29.7703 20.2146 33.4025 26.7842 23.5166 29.4057C23.5482 29.3741 23.6429 29.2794 23.6745 29.1846Z" fill="#0074BD">
</path>
<path d="M19.2206 0.25293C19.2206 0.25293 23.3266 4.35891 15.3357 10.6758C8.92404 15.7293 13.8828 18.6351 15.3357 21.9199C11.6087 18.5403 8.86088 15.5714 10.6928 12.792C13.3775 8.74919 20.863 6.79093 19.2206 0.25293Z" fill="#EA2D2E">
</path>
<path d="M11.5455 39.6068C18.6521 40.049 29.5803 39.3541 29.833 35.9746C29.833 35.9746 29.3276 37.238 23.9583 38.2487C17.894 39.3858 10.4085 39.2594 5.95508 38.533C5.95508 38.5646 6.87103 39.3225 11.5455 39.6068Z" fill="#0074BD">
</path>
</svg>
<span>
Java Sync
</span>
</a>
<a class="leafygreen-ui-v22cck" href="https://mongodb.github.io/node-mongodb-native/6.9/classes/FindCursor.html#explain" target="_self">
<svg class="leafygreen-ui-x3zji3" fill="none" height="24" viewbox="0 0 30 30" width="24" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:R2dt5qcmqn9:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R2dt5qcmqn9:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":R2dt5qcmqn9:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":R2dt5qcmqn9:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<span>
Node.js
</span>
</a>
<a class="leafygreen-ui-v22cck" href="https://www.mongodb.com/docs/languages/python/pymongo-driver/current/aggregation/#explain-an-aggregation" target="_self">
<svg class="leafygreen-ui-x3zji3" height="24" viewbox="0 0 111.6 147" width="24" xmlns="http://www.w3.org/2000/svg">
<lineargradient gradienttransform="matrix(.5625 0 0 -.568 -160.323 -146.09)" gradientunits="userSpaceOnUse" id=":R2ht5qcmqn9:aa" x1="280.848" x2="389.865" y1="-268.463" y2="-362.257">
<stop offset="0" stop-color="#5a9fd4">
</stop>
<stop offset="1" stop-color="#306998">
</stop>
</lineargradient>
<path d="M55.3 3.2c-4.6 0-9 .4-12.8 1.1-11.3 2-13.4 6.2-13.4 13.9v10.2h26.8v3.4H19c-7.8 0-14.6 4.7-16.8 13.6C-.3 55.6-.4 62 2.2 72.7c1.9 7.9 6.5 13.6 14.2 13.6h9.2V74c0-8.8 7.7-16.7 16.7-16.7h26.8c7.5 0 13.4-6.1 13.4-13.6V18.2c0-7.3-6.1-12.7-13.4-13.9-4.4-.8-9.2-1.1-13.8-1.1zm-14.5 8.2c2.8 0 5 2.3 5 5.1s-2.3 5.1-5 5.1c-2.8 0-5-2.3-5-5.1s2.2-5.1 5-5.1z" fill="url(#:R2ht5qcmqn9:aa)">
</path>
<lineargradient gradienttransform="matrix(.5625 0 0 -.568 -160.323 -146.09)" gradientunits="userSpaceOnUse" id=":R2ht5qcmqn9:bb" x1="432.011" x2="393.081" y1="-419.025" y2="-363.946">
<stop offset="0" stop-color="#ffd43b">
</stop>
<stop offset="1" stop-color="#ffe873">
</stop>
</lineargradient>
<path d="M86 31.8v11.9c0 9.2-7.8 17-16.8 17H42.5c-7.3 0-13.4 6.3-13.4 13.6v25.5c0 7.3 6.3 11.5 13.4 13.6 8.5 2.5 16.6 2.9 26.8 0 6.8-2 13.4-5.9 13.4-13.6V89.7H55.9v-3.4h40.2c7.8 0 10.7-5.4 13.4-13.6 2.8-8.4 2.7-16.5 0-27.3-1.9-7.8-5.6-13.6-13.4-13.6H86zM71 96.5c2.8 0 5 2.3 5 5.1s-2.3 5.1-5 5.1c-2.8 0-5-2.3-5-5.1-.1-2.8 2.2-5.1 5-5.1z" fill="url(#:R2ht5qcmqn9:bb)">
</path>
<radialgradient cx="-2274.721" cy="144.526" gradienttransform="matrix(0 -.2399 -1.0547 0 208.637 -414.922)" gradientunits="userSpaceOnUse" id=":R2ht5qcmqn9:cc" r="29.037">
<stop offset="0" stop-color="#b8b8b8" stop-opacity=".498">
</stop>
<stop offset="1" stop-color="#7f7f7f" stop-opacity="0">
</stop>
</radialgradient>
<path d="M92.1 130.9c0 3.8-16.1 7-35.9 7s-35.9-3.1-35.9-7c0-3.8 16.1-7 35.9-7s35.9 3.1 35.9 7z" fill="url(#:R2ht5qcmqn9:cc)" opacity=".444">
</path>
</svg>
<span>
PyMongo
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://mongoc.org/libmongoc/current/mongoc_collection_find.html#the-explain-command" target="_self">
<svg class="leafygreen-ui-x3zji3" fill="none" height="24" viewbox="0 0 28 29" width="24" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59955)">
<g clip-path="url(#clip1_1351_59955)">
<path clip-rule="evenodd" d="M13.118 1.49532C13.5403 1.25103 14.0559 1.25103 14.4783 1.49532C16.5652 2.70389 22.8385 6.34889 24.9193 7.55748C25.3416 7.80172 25.6025 8.25176 25.6025 8.74672C25.6025 11.1703 25.6025 18.4539 25.6025 20.8774C25.6025 21.366 25.3416 21.8224 24.9193 22.0667C22.8385 23.2753 16.5652 26.9203 14.4783 28.1289C14.0559 28.3732 13.5403 28.3732 13.118 28.1289C11.0373 26.9139 4.76398 23.2689 2.68323 22.0603C2.26087 21.816 2 21.366 2 20.871C2 18.4474 2 11.1639 2 8.7403C2 8.25176 2.26087 7.7953 2.68323 7.55106C4.76398 6.34889 11.0373 2.70389 13.118 1.49532Z" fill="#004482" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M2.18634 21.4946C2.01863 21.2632 2 21.051 2 20.7553C2 18.351 2 11.1061 2 8.70176C2 8.21317 2.26087 7.76319 2.67702 7.51889C4.75776 6.31675 11.0062 2.69104 13.0807 1.4889C13.5031 1.24461 14.0683 1.25104 14.4907 1.49533C16.5652 2.69747 22.795 6.29104 24.8696 7.49319C25.0373 7.58963 25.1677 7.70535 25.2795 7.86606L2.18634 21.4946Z" fill="#659AD2" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M13.8011 5.8089C18.6024 5.8089 22.4968 9.83964 22.4968 14.8089C22.4968 19.7782 18.6024 23.8089 13.8011 23.8089C8.99986 23.8089 5.10547 19.7782 5.10547 14.8089C5.10547 9.83964 8.99986 5.8089 13.8011 5.8089ZM13.8011 10.3089C16.1987 10.3089 18.1489 12.3275 18.1489 14.8089C18.1489 17.2903 16.1987 19.3089 13.8011 19.3089C11.4036 19.3089 9.4533 17.2903 9.4533 14.8089C9.4533 12.3275 11.4036 10.3089 13.8011 10.3089Z" fill="white" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M25.2857 7.87247C25.6087 8.18102 25.6025 8.65034 25.6025 9.01673C25.6025 11.4596 25.5839 18.396 25.6087 20.8325C25.6087 21.0896 25.528 21.3532 25.4099 21.5589L13.5466 14.8089L25.2857 7.87247Z" fill="#00599C" fill-rule="evenodd">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59955">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59955">
<rect fill="white" height="27" transform="translate(2 1.30887)" width="24">
</rect>
</clippath>
</defs>
</svg>
<span>
C
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.mongodb.com/docs/languages/cpp/cpp-driver/upcoming/aggregation/#explain-an-aggregation" target="_self">
<svg class="leafygreen-ui-x3zji3" fill="none" height="24" viewbox="0 0 28 29" width="24" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_1351_59959)">
<g clip-path="url(#clip1_1351_59959)">
<path d="M25.4013 21.5646C25.5872 21.2353 25.7034 20.8745 25.7034 20.5373V9.08012C25.7034 8.75078 25.5872 8.38217 25.4013 8.05283L13.8516 14.8127L25.4013 21.5646Z" fill="#00599C">
</path>
<path d="M14.8738 28.0657L24.6729 22.3411C24.9517 22.1764 25.2151 21.8941 25.401 21.5647L13.8513 14.8127L2.30151 21.5647C2.48743 21.8941 2.74305 22.1764 3.02967 22.3411L12.8288 28.0657C13.3865 28.3951 14.316 28.3951 14.8738 28.0657Z" fill="#004482">
</path>
<path d="M25.4011 8.06086C25.2152 7.73146 24.9596 7.44915 24.673 7.2845L14.8739 1.55983C14.3084 1.23046 13.3866 1.23046 12.8211 1.55983L3.02202 7.2845C2.46429 7.61385 1.99951 8.42158 1.99951 9.08033V20.5375C1.99951 20.8669 2.11571 21.2354 2.30162 21.5647L13.8514 14.8128L25.4011 8.06086Z" fill="#659AD2">
</path>
<path d="M13.8512 22.8115C9.49778 22.8115 5.94995 19.2199 5.94995 14.8127C5.94995 10.4055 9.49778 6.81387 13.8512 6.81387C16.6631 6.81387 19.2813 8.34305 20.6912 10.8133L17.2751 12.813C16.5701 11.5818 15.261 10.8133 13.8512 10.8133C11.6745 10.8133 9.90055 12.6091 9.90055 14.8127C9.90055 17.0163 11.6745 18.8121 13.8512 18.8121C15.261 18.8121 16.5701 18.0436 17.2751 16.8124L20.6912 18.8121C19.2813 21.2745 16.6631 22.8115 13.8512 22.8115Z" fill="white">
</path>
<path d="M21.7524 14.3657H20.8771V13.4796H19.994V14.3657H19.1187V15.2518H19.994V16.1458H20.8771V15.2518H21.7524V14.3657Z" fill="white">
</path>
<path d="M25.0446 14.3657H24.1693V13.4796H23.2862V14.3657H22.4109V15.2518H23.2862V16.1458H24.1693V15.2518H25.0446V14.3657Z" fill="white">
</path>
</g>
</g>
<defs>
<clippath id="clip0_1351_59959">
<rect fill="white" height="28" transform="translate(0 0.809265)" width="28">
</rect>
</clippath>
<clippath id="clip1_1351_59959">
<rect fill="white" height="27" transform="translate(1.99951 1.30878)" width="24">
</rect>
</clippath>
</defs>
</svg>
<span>
C++
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.mongodb.com/docs/drivers/go/current/fundamentals/run-command/#example" target="_self">
<svg class="leafygreen-ui-x3zji3" height="24" id="Layer_1" style="fill:#00ACD7" version="1.1" viewbox="0 0 205.4 76.7" width="24" x="0" xmlns="http://www.w3.org/2000/svg" y="0">
<path class="st00" d="M15.5 23.2c-.4 0-.5-.2-.3-.5l2.1-2.7c.2-.3.7-.5 1.1-.5h35.7c.4 0 .5.3.3.6l-1.7 2.6c-.2.3-.7.6-1 .6l-36.2-.1zM.4 32.4c-.4 0-.5-.2-.3-.5l2.1-2.7c.2-.3.7-.5 1.1-.5h45.6c.4 0 .6.3.5.6l-.8 2.4c-.1.4-.5.6-.9.6l-47.3.1zM24.6 41.6c-.4 0-.5-.3-.3-.6l1.4-2.5c.2-.3.6-.6 1-.6h20c.4 0 .6.3.6.7l-.2 2.4c0 .4-.4.7-.7.7l-21.8-.1z">
</path>
<g id="CXHf1q_3_">
<path class="st00" d="M128.4 21.4c-6.3 1.6-10.6 2.8-16.8 4.4-1.5.4-1.6.5-2.9-1-1.5-1.7-2.6-2.8-4.7-3.8-6.3-3.1-12.4-2.2-18.1 1.5-6.8 4.4-10.3 10.9-10.2 19 .1 8 5.6 14.6 13.5 15.7 6.8.9 12.5-1.5 17-6.6.9-1.1 1.7-2.3 2.7-3.7H89.6c-2.1 0-2.6-1.3-1.9-3 1.3-3.1 3.7-8.3 5.1-10.9.3-.6 1-1.6 2.5-1.6h36.4c-.2 2.7-.2 5.4-.6 8.1-1.1 7.2-3.8 13.8-8.2 19.6-7.2 9.5-16.6 15.4-28.5 17-9.8 1.3-18.9-.6-26.9-6.6-7.4-5.6-11.6-13-12.7-22.2-1.3-10.9 1.9-20.7 8.5-29.3C70.4 8.7 79.8 2.8 91.3.7c9.4-1.7 18.4-.6 26.5 4.9 5.3 3.5 9.1 8.3 11.6 14.1.6.9.2 1.4-1 1.7z">
</path>
<path class="st00" d="M161.5 76.7c-9.1-.2-17.4-2.8-24.4-8.8-5.9-5.1-9.6-11.6-10.8-19.3-1.8-11.3 1.3-21.3 8.1-30.2 7.3-9.6 16.1-14.6 28-16.7 10.2-1.8 19.8-.8 28.5 5.1 7.9 5.4 12.8 12.7 14.1 22.3 1.7 13.5-2.2 24.5-11.5 33.9-6.6 6.7-14.7 10.9-24 12.8-2.7.5-5.4.6-8 .9zm23.8-40.4c-.1-1.3-.1-2.3-.3-3.3-1.8-9.9-10.9-15.5-20.4-13.3-9.3 2.1-15.3 8-17.5 17.4-1.8 7.8 2 15.7 9.2 18.9 5.5 2.4 11 2.1 16.3-.6 7.9-4.1 12.2-10.5 12.7-19.1z">
</path>
</g>
</svg>
<span>
Go
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.mongodb.com/docs/languages/java/reactive-streams-driver/current/tutorials/aggregation/#explain-an-aggregation" target="_self">
<svg class="leafygreen-ui-x3zji3" fill="none" height="24" viewbox="0 0 30 41" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M10.0921 30.7949C10.0921 30.7949 8.57608 31.6793 11.166 31.9636C14.2929 32.311 15.9037 32.2794 19.3464 31.6161C19.3464 31.6161 20.2623 32.1846 21.5257 32.6901C13.7876 35.9748 4.02791 32.5005 10.0921 30.7949Z" fill="#0074BD">
</path>
<path d="M9.14539 26.4678C9.14539 26.4678 7.43985 27.7312 10.0298 27.9838C13.3778 28.3312 15.9992 28.3628 20.579 27.4785C20.579 27.4785 21.2107 28.1101 22.1898 28.4576C12.8724 31.2054 2.44949 28.6787 9.14539 26.4678Z" fill="#0074BD">
</path>
<path d="M17.1039 19.1398C18.9989 21.3191 16.5985 23.3089 16.5985 23.3089C16.5985 23.3089 21.431 20.8138 19.2201 17.6869C17.1355 14.7811 15.5563 13.3282 24.1788 8.33789C24.1473 8.33789 10.6607 11.6858 17.1039 19.1398Z" fill="#EA2D2E">
</path>
<path d="M27.3377 33.9845C27.3377 33.9845 28.4431 34.9004 26.1059 35.6269C21.6525 36.985 7.53417 37.3957 3.61769 35.6901C2.19639 35.0899 4.84949 34.2372 5.67069 34.0477C6.52347 33.8582 7.02884 33.8898 7.02884 33.8898C5.44961 32.7843 -3.10981 36.0691 2.67016 36.9851C18.4308 39.5433 31.4121 35.8164 27.3377 33.9845Z" fill="#0074BD">
</path>
<path d="M10.8194 21.983C10.8194 21.983 3.6497 23.6885 8.2926 24.3203C10.2509 24.5729 14.1357 24.5097 17.7995 24.2255C20.7685 23.9728 23.7375 23.4359 23.7375 23.4359C23.7375 23.4359 22.6951 23.8781 21.9372 24.415C14.6411 26.3416 0.585992 25.4257 4.62881 23.4675C8.00839 21.7935 10.8194 21.983 10.8194 21.983Z" fill="#0074BD">
</path>
<path d="M23.6745 29.1846C31.0969 25.3313 27.6542 21.6359 25.2538 22.1413C24.6537 22.2676 24.401 22.3623 24.401 22.3623C24.401 22.3623 24.6221 22.0149 25.0327 21.8886C29.7703 20.2146 33.4025 26.7842 23.5166 29.4057C23.5482 29.3741 23.6429 29.2794 23.6745 29.1846Z" fill="#0074BD">
</path>
<path d="M19.2206 0.25293C19.2206 0.25293 23.3266 4.35891 15.3357 10.6758C8.92404 15.7293 13.8828 18.6351 15.3357 21.9199C11.6087 18.5403 8.86088 15.5714 10.6928 12.792C13.3775 8.74919 20.863 6.79093 19.2206 0.25293Z" fill="#EA2D2E">
</path>
<path d="M11.5455 39.6068C18.6521 40.049 29.5803 39.3541 29.833 35.9746C29.833 35.9746 29.3276 37.238 23.9583 38.2487C17.894 39.3858 10.4085 39.2594 5.95508 38.533C5.95508 38.5646 6.87103 39.3225 11.5455 39.6068Z" fill="#0074BD">
</path>
</svg>
<span>
Java RS
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://mongodb.github.io/mongo-java-driver/5.2/apidocs/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-aggregate-flow/explain.html?query=suspend%20fun%20%3CR%20:%20Any%3E%20explain(resultClass:%20Class%3CR%3E,%20verbosity:%20ExplainVerbosity?%20=%20null):%20R" target="_self">
<svg class="leafygreen-ui-x3zji3" fill="none" height="24" viewbox="0 0 28 29" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M28 29H0V1H28L14 15L28 29Z" fill="url(#:R35t5qcmqn9:paint0_linear_211_40588)">
</path>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R35t5qcmqn9:paint0_linear_211_40588" x1="28" x2="8.34465e-07" y1="0.999999" y2="29">
<stop offset="0.00343514" stop-color="#E44857">
</stop>
<stop offset="0.4689" stop-color="#C711E1">
</stop>
<stop offset="1" stop-color="#7F52FF">
</stop>
</lineargradient>
</defs>
</svg>
<span>
Kotlin Coroutine
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://mongodb.github.io/mongo-java-driver/5.2/apidocs/mongodb-driver-kotlin-sync/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-aggregate-iterable/explain.html?query=fun%20%3CR%20:%20Any%3E%20explain(resultClass:%20Class%3CR%3E,%20verbosity:%20ExplainVerbosity?%20=%20null):%20R" target="_self">
<svg class="leafygreen-ui-x3zji3" fill="none" height="24" viewbox="0 0 28 29" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M28 29H0V1H28L14 15L28 29Z" fill="url(#:R39t5qcmqn9:paint0_linear_211_40588)">
</path>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R39t5qcmqn9:paint0_linear_211_40588" x1="28" x2="8.34465e-07" y1="0.999999" y2="29">
<stop offset="0.00343514" stop-color="#E44857">
</stop>
<stop offset="0.4689" stop-color="#C711E1">
</stop>
<stop offset="1" stop-color="#7F52FF">
</stop>
</lineargradient>
</defs>
</svg>
<span>
Kotlin Sync
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.mongodb.com/docs/php-library/current/reference/method/MongoDBCollection-explain/" target="_self">
<svg class="leafygreen-ui-x3zji3" height="24" viewbox="0 0 700 301" width="24" x="0" xmlns="http://www.w3.org/2000/svg" y="0">
<defs>
<clippath clippathunits="userSpaceOnUse" id="a">
<path d="M11.52 162C11.52 81.677 135.307 16.561 288 16.561S564.481 81.677 564.481 162c0 80.322-123.788 145.439-276.481 145.439-152.693 0-276.48-65.117-276.48-145.439">
</path>
</clippath>
<clippath clippathunits="userSpaceOnUse" id="c">
<path d="M0 324h576V0H0v324z">
</path>
</clippath>
<clippath clippathunits="userSpaceOnUse" id="d">
<path d="M0 324h576V0H0v324z">
</path>
</clippath>
<radialgradient cx="0" cy="0" fx="0" fy="0" gradienttransform="matrix(363.05789 0 0 -363.05789 177.52 256.307)" gradientunits="userSpaceOnUse" id="b" r="1" spreadmethod="pad">
<stop offset="0" stop-color="#aeb2d5">
</stop>
<stop offset=".3" stop-color="#aeb2d5">
</stop>
<stop offset=".75" stop-color="#484c89">
</stop>
<stop offset="1" stop-color="#484c89">
</stop>
</radialgradient>
</defs>
<g clip-path="url(#a)" transform="matrix(1.25 0 0 -1.25 -4.4 394.299)">
<path d="M11.52 162C11.52 81.677 135.307 16.561 288 16.561S564.481 81.677 564.481 162c0 80.322-123.788 145.439-276.481 145.439-152.693 0-276.48-65.117-276.48-145.439" fill="url(#b)">
</path>
</g>
<g clip-path="url(#c)" transform="matrix(1.25 0 0 -1.25 -4.4 394.299)">
<path d="M288 27.36c146.729 0 265.68 60.28 265.68 134.64 0 74.36-118.951 134.641-265.68 134.641S22.32 236.36 22.32 162.001C22.32 87.64 141.271 27.358 288 27.358" fill="#777bb3">
</path>
</g>
<g clip-path="url(#d)" transform="matrix(1.25 0 0 -1.25 -4.4 394.299)">
<path d="M161.734 145.307c12.065 0 21.072 2.225 26.771 6.61 5.638 4.342 9.532 11.863 11.573 22.354 1.903 9.806 1.178 16.653-2.154 20.348-3.407 3.774-10.773 5.688-21.893 5.688h-19.28l-10.69-55h15.673zm-63.063-67.75a2.998 2.998 0 00-2.944 3.572l28.328 145.75a3 3 0 002.945 2.428h61.054c19.188 0 33.47-5.21 42.447-15.487 9.025-10.331 11.812-24.772 8.283-42.921-1.436-7.394-3.906-14.261-7.34-20.41-3.44-6.154-7.985-11.85-13.512-16.93-6.616-6.191-14.104-10.681-22.236-13.323-8.003-2.607-18.28-3.93-30.548-3.93h-24.722l-7.06-36.321a3 3 0 00-2.944-2.428h-31.75z">
</path>
<path d="M159.224 197.307h16.808c13.42 0 18.083-2.945 19.667-4.7 2.628-2.914 3.124-9.058 1.435-17.767-1.898-9.75-5.416-16.663-10.458-20.545-5.162-3.974-13.554-5.988-24.941-5.988H149.7l9.523 49zm28.83 35H127a6 6 0 01-5.89-4.855L92.784 81.702a6 6 0 015.889-7.144h31.75a6 6 0 015.89 4.854l6.588 33.895h22.249c12.582 0 23.174 1.372 31.479 4.077 8.54 2.775 16.399 7.48 23.354 13.984 5.752 5.292 10.49 11.232 14.08 17.657 3.59 6.427 6.17 13.594 7.668 21.302 3.715 19.104.697 34.402-8.97 45.466-9.571 10.958-24.613 16.514-44.705 16.514m-45.633-90h19.313c12.8 0 22.336 2.411 28.6 7.234 6.267 4.824 10.493 12.875 12.689 24.157 2.1 10.832 1.144 18.476-2.871 22.93-4.02 4.452-12.06 6.68-24.121 6.68h-21.754l-11.856-61m45.633 84c18.367 0 31.766-4.82 40.188-14.462 8.42-9.64 10.957-23.098 7.597-40.375-1.383-7.117-3.722-13.624-7.015-19.519-3.297-5.899-7.602-11.293-12.922-16.184-6.34-5.933-13.383-10.16-21.133-12.679-7.75-2.525-17.621-3.782-29.621-3.782h-27.196l-7.531-38.75h-31.75L127 226.307h61.055" fill="#fff">
</path>
<path d="M311.583 116.307c-.896 0-1.745.4-2.314 1.092a2.994 2.994 0 00-.631 2.48l12.531 64.489c1.192 6.133.898 10.535-.827 12.395-1.056 1.137-4.228 3.044-13.607 3.044h-22.702l-15.755-81.072a3 3 0 00-2.945-2.428h-31.5a2.998 2.998 0 00-2.945 3.572l28.328 145.75a3 3 0 002.945 2.428h31.5a3 3 0 002.945-3.572l-6.836-35.178h24.422c18.605 0 31.221-3.28 38.569-10.028 7.49-6.884 9.827-17.891 6.947-32.72l-13.18-67.824a3 3 0 00-2.945-2.428h-32z">
</path>
<path d="M293.661 271.057h-31.5a6 6 0 01-5.89-4.855l-28.328-145.75a5.998 5.998 0 015.89-7.145h31.5a6 6 0 015.89 4.855l15.283 78.645h20.23c9.362 0 11.327-2 11.406-2.086.568-.61 1.315-3.44.082-9.78l-12.53-64.49a5.998 5.998 0 015.89-7.144h32a6 6 0 015.89 4.855l13.178 67.825c3.093 15.921.447 27.864-7.86 35.5-7.929 7.281-21.209 10.82-40.6 10.82h-20.784l6.143 31.605a6.001 6.001 0 01-5.89 7.145m0-6l-7.53-38.75h28.061c17.657 0 29.836-3.082 36.54-9.238 6.702-6.16 8.71-16.14 6.031-29.938l-13.18-67.824h-32l12.531 64.488c1.426 7.336.902 12.34-1.574 15.008-2.477 2.668-7.746 4.004-15.805 4.004H281.56l-16.226-83.5h-31.5l28.328 145.75h31.5" fill="#fff">
</path>
<path d="M409.55 145.307c12.065 0 21.072 2.225 26.77 6.61 5.639 4.34 9.533 11.862 11.575 22.354 1.903 9.806 1.178 16.653-2.155 20.348-3.407 3.774-10.773 5.688-21.893 5.688h-19.281l-10.69-55h15.674zm-63.062-67.75a2.999 2.999 0 00-2.945 3.572l28.328 145.75a3.002 3.002 0 002.946 2.428h61.053c19.189 0 33.47-5.21 42.448-15.487 9.025-10.33 11.81-24.771 8.283-42.921-1.438-7.394-3.907-14.261-7.342-20.41-3.44-6.154-7.984-11.85-13.511-16.93-6.616-6.191-14.104-10.681-22.236-13.323-8.003-2.607-18.281-3.93-30.548-3.93H388.24l-7.057-36.321a3.001 3.001 0 00-2.946-2.428h-31.75z">
</path>
<path d="M407.04 197.307h16.807c13.421 0 18.083-2.945 19.667-4.7 2.63-2.914 3.125-9.058 1.435-17.766-1.898-9.75-5.417-16.664-10.458-20.546-5.162-3.974-13.554-5.988-24.94-5.988h-12.034l9.522 49zm28.83 35h-61.054a6 6 0 01-5.889-4.855L340.6 81.702a6 6 0 015.889-7.144h31.75a6 6 0 015.89 4.854l6.587 33.895h22.25c12.581 0 23.173 1.372 31.478 4.077 8.541 2.775 16.401 7.481 23.356 13.986 5.752 5.291 10.488 11.23 14.078 17.655 3.591 6.427 6.171 13.594 7.668 21.302 3.715 19.105.697 34.403-8.969 45.467-9.572 10.957-24.613 16.513-44.706 16.513m-45.632-90h19.312c12.801 0 22.336 2.411 28.601 7.234 6.267 4.824 10.492 12.875 12.688 24.157 2.102 10.832 1.145 18.476-2.87 22.93-4.02 4.452-12.06 6.68-24.122 6.68h-21.754l-11.855-61m45.632 84c18.367 0 31.766-4.82 40.188-14.462 8.422-9.64 10.957-23.098 7.597-40.375-1.383-7.117-3.722-13.624-7.015-19.519-3.297-5.899-7.602-11.293-12.922-16.184-6.34-5.933-13.383-10.16-21.133-12.679-7.75-2.525-17.62-3.782-29.62-3.782h-27.197l-7.53-38.75h-31.75l28.328 145.75h61.054" fill="#fff">
</path>
</g>
</svg>
<span>
PHP
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.rubydoc.info/github/mongoid/mongoid/Mongoid%2FContextual%2FMongo:explain" target="_self">
<svg class="leafygreen-ui-x3zji3" height="24" preserveaspectratio="xMidYMid" viewbox="0 0 300 240" width="24" xmlns="http://www.w3.org/2000/svg">
<defs>
<lineargradient id=":R3ht5qcmqn9:a" x1="84.75%" x2="58.254%" y1="111.399%" y2="64.584%">
<stop offset="0%" stop-color="#FB7655">
</stop>
<stop offset="0%" stop-color="#FB7655">
</stop>
<stop offset="41%" stop-color="#E42B1E">
</stop>
<stop offset="99%" stop-color="#900">
</stop>
<stop offset="100%" stop-color="#900">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:b" x1="116.651%" x2="1.746%" y1="60.89%" y2="19.288%">
<stop offset="0%" stop-color="#871101">
</stop>
<stop offset="0%" stop-color="#871101">
</stop>
<stop offset="99%" stop-color="#911209">
</stop>
<stop offset="100%" stop-color="#911209">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:c" x1="75.774%" x2="38.978%" y1="219.327%" y2="7.829%">
<stop offset="0%" stop-color="#871101">
</stop>
<stop offset="0%" stop-color="#871101">
</stop>
<stop offset="99%" stop-color="#911209">
</stop>
<stop offset="100%" stop-color="#911209">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:d" x1="50.012%" x2="66.483%" y1="7.234%" y2="79.135%">
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="23%" stop-color="#E57252">
</stop>
<stop offset="46%" stop-color="#DE3B20">
</stop>
<stop offset="99%" stop-color="#A60003">
</stop>
<stop offset="100%" stop-color="#A60003">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:e" x1="46.174%" x2="49.932%" y1="16.348%" y2="83.047%">
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="23%" stop-color="#E4714E">
</stop>
<stop offset="56%" stop-color="#BE1A0D">
</stop>
<stop offset="99%" stop-color="#A80D00">
</stop>
<stop offset="100%" stop-color="#A80D00">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:f" x1="36.965%" x2="49.528%" y1="15.594%" y2="92.478%">
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="18%" stop-color="#E46342">
</stop>
<stop offset="40%" stop-color="#C82410">
</stop>
<stop offset="99%" stop-color="#A80D00">
</stop>
<stop offset="100%" stop-color="#A80D00">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:g" x1="13.609%" x2="85.764%" y1="58.346%" y2="-46.717%">
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="54%" stop-color="#C81F11">
</stop>
<stop offset="99%" stop-color="#BF0905">
</stop>
<stop offset="100%" stop-color="#BF0905">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:h" x1="27.624%" x2="50.745%" y1="21.135%" y2="79.056%">
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="0%" stop-color="#FFF">
</stop>
<stop offset="31%" stop-color="#DE4024">
</stop>
<stop offset="99%" stop-color="#BF190B">
</stop>
<stop offset="100%" stop-color="#BF190B">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:i" x1="-20.667%" x2="104.242%" y1="122.282%" y2="-6.342%">
<stop offset="0%" stop-color="#BD0012">
</stop>
<stop offset="0%" stop-color="#BD0012">
</stop>
<stop offset="7%" stop-color="#FFF">
</stop>
<stop offset="17%" stop-color="#FFF">
</stop>
<stop offset="27%" stop-color="#C82F1C">
</stop>
<stop offset="33%" stop-color="#820C01">
</stop>
<stop offset="46%" stop-color="#A31601">
</stop>
<stop offset="72%" stop-color="#B31301">
</stop>
<stop offset="99%" stop-color="#E82609">
</stop>
<stop offset="100%" stop-color="#E82609">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:j" x1="58.792%" x2="11.964%" y1="65.205%" y2="50.128%">
<stop offset="0%" stop-color="#8C0C01">
</stop>
<stop offset="0%" stop-color="#8C0C01">
</stop>
<stop offset="54%" stop-color="#990C00">
</stop>
<stop offset="99%" stop-color="#A80D0E">
</stop>
<stop offset="100%" stop-color="#A80D0E">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:k" x1="79.319%" x2="23.088%" y1="62.754%" y2="17.888%">
<stop offset="0%" stop-color="#7E110B">
</stop>
<stop offset="0%" stop-color="#7E110B">
</stop>
<stop offset="99%" stop-color="#9E0C00">
</stop>
<stop offset="100%" stop-color="#9E0C00">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:l" x1="92.88%" x2="59.841%" y1="74.122%" y2="39.704%">
<stop offset="0%" stop-color="#79130D">
</stop>
<stop offset="0%" stop-color="#79130D">
</stop>
<stop offset="99%" stop-color="#9E120B">
</stop>
<stop offset="100%" stop-color="#9E120B">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:o" x1="56.57%" x2="3.105%" y1="101.717%" y2="11.993%">
<stop offset="0%" stop-color="#8B2114">
</stop>
<stop offset="0%" stop-color="#8B2114">
</stop>
<stop offset="43%" stop-color="#9E100A">
</stop>
<stop offset="99%" stop-color="#B3100C">
</stop>
<stop offset="100%" stop-color="#B3100C">
</stop>
</lineargradient>
<lineargradient id=":R3ht5qcmqn9:p" x1="30.87%" x2="92.471%" y1="35.599%" y2="100.694%">
<stop offset="0%" stop-color="#B31000">
</stop>
<stop offset="0%" stop-color="#B31000">
</stop>
<stop offset="44%" stop-color="#910F08">
</stop>
<stop offset="99%" stop-color="#791C12">
</stop>
<stop offset="100%" stop-color="#791C12">
</stop>
</lineargradient>
<radialgradient cx="32.001%" cy="40.21%" fx="32.001%" fy="40.21%" id=":R3ht5qcmqn9:m" r="69.573%">
<stop offset="0%" stop-color="#A80D00">
</stop>
<stop offset="0%" stop-color="#A80D00">
</stop>
<stop offset="99%" stop-color="#7E0E08">
</stop>
<stop offset="100%" stop-color="#7E0E08">
</stop>
</radialgradient>
<radialgradient cx="13.549%" cy="40.86%" fx="13.549%" fy="40.86%" id=":R3ht5qcmqn9:n" r="88.386%">
<stop offset="0%" stop-color="#A30C00">
</stop>
<stop offset="0%" stop-color="#A30C00">
</stop>
<stop offset="99%" stop-color="#800E08">
</stop>
<stop offset="100%" stop-color="#800E08">
</stop>
</radialgradient>
</defs>
<path d="M197.467 167.764l-145.52 86.41 188.422-12.787L254.88 51.393l-57.414 116.37z" fill="url(#:R3ht5qcmqn9:a)">
</path>
<path d="M240.677 241.257L224.482 129.48l-44.113 58.25 60.308 53.528z" fill="url(#:R3ht5qcmqn9:b)">
</path>
<path d="M240.896 241.257l-118.646-9.313-69.674 21.986 188.32-12.673z" fill="url(#:R3ht5qcmqn9:c)">
</path>
<path d="M52.744 253.955l29.64-97.1L17.16 170.8l35.583 83.154z" fill="url(#:R3ht5qcmqn9:d)">
</path>
<path d="M180.358 188.05L153.085 81.226l-78.047 73.16 105.32 33.666z" fill="url(#:R3ht5qcmqn9:e)">
</path>
<path d="M248.693 82.73l-73.777-60.256-20.544 66.418 94.321-6.162z" fill="url(#:R3ht5qcmqn9:f)">
</path>
<path d="M214.191.99L170.8 24.97 143.424.669l70.767.322z" fill="url(#:R3ht5qcmqn9:g)">
</path>
<path d="M0 203.372l18.177-33.151-14.704-39.494L0 203.372z" fill="url(#:R3ht5qcmqn9:h)">
</path>
<path d="M2.496 129.48l14.794 41.963 64.283-14.422 73.39-68.207 20.712-65.787L143.063 0 87.618 20.75c-17.469 16.248-51.366 48.396-52.588 49-1.21.618-22.384 40.639-32.534 59.73z" fill="#FFF">
</path>
<path d="M54.442 54.094c37.86-37.538 86.667-59.716 105.397-40.818 18.72 18.898-1.132 64.823-38.992 102.349-37.86 37.525-86.062 60.925-104.78 42.027-18.73-18.885.515-66.032 38.375-103.558z" fill="url(#:R3ht5qcmqn9:i)">
</path>
<path d="M52.744 253.916l29.408-97.409 97.665 31.376c-35.312 33.113-74.587 61.106-127.073 66.033z" fill="url(#:R3ht5qcmqn9:j)">
</path>
<path d="M155.092 88.622l25.073 99.313c29.498-31.016 55.972-64.36 68.938-105.603l-94.01 6.29z" fill="url(#:R3ht5qcmqn9:k)">
</path>
<path d="M248.847 82.833c10.035-30.282 12.35-73.725-34.966-81.791l-38.825 21.445 73.791 60.346z" fill="url(#:R3ht5qcmqn9:l)">
</path>
<path d="M0 202.935c1.39 49.979 37.448 50.724 52.808 51.162l-35.48-82.86L0 202.935z" fill="#9E1209">
</path>
<path d="M155.232 88.777c22.667 13.932 68.35 41.912 69.276 42.426 1.44.81 19.695-30.784 23.838-48.64l-93.114 6.214z" fill="url(#:R3ht5qcmqn9:m)">
</path>
<path d="M82.113 156.507l39.313 75.848c23.246-12.607 41.45-27.967 58.121-44.42l-97.434-31.428z" fill="url(#:R3ht5qcmqn9:n)">
</path>
<path d="M17.174 171.34l-5.57 66.328c10.51 14.357 24.97 15.605 40.136 14.486-10.973-27.311-32.894-81.92-34.566-80.814z" fill="url(#:R3ht5qcmqn9:o)">
</path>
<path d="M174.826 22.654l78.1 10.96c-4.169-17.662-16.969-29.06-38.787-32.623l-39.313 21.663z" fill="url(#:R3ht5qcmqn9:p)">
</path>
</svg>
<span>
Mongoid
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.mongodb.com/docs/drivers/rust/current/fundamentals/run-command/#command-example" target="_self">
<svg class="leafygreen-ui-x3zji3" height="24" viewbox="0 0 30 30" width="24" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_5454_14623)">
<path d="M15.2561 3.08398C8.56014 3.08398 3.11035 8.53634 3.11035 15.2297C3.11035 21.9231 8.56271 27.3755 15.2561 27.3755C21.9495 27.3755 27.4019 21.9231 27.4019 15.2297C27.4019 8.53634 21.9495 3.08398 15.2561 3.08398ZM15.2381 4.16315C15.4458 4.16853 15.6432 4.25509 15.7881 4.40437C15.9331 4.55365 16.0142 4.75384 16.0141 4.96225C16.0141 5.17418 15.9299 5.37744 15.7801 5.5273C15.6302 5.67715 15.4269 5.76134 15.215 5.76134C15.0031 5.76134 14.7998 5.67715 14.65 5.5273C14.5001 5.37744 14.4159 5.17418 14.4159 4.96225C14.4159 4.85533 14.4373 4.7495 14.4789 4.65101C14.5205 4.55252 14.5814 4.46338 14.6581 4.38887C14.7347 4.31435 14.8256 4.25598 14.9252 4.21721C15.0249 4.17844 15.1313 4.16006 15.2381 4.16315ZM17.0676 5.47871C18.4704 5.74146 19.7992 6.30644 20.9617 7.13439C22.1242 7.96235 23.0926 9.03345 23.7995 10.2733L22.8565 12.4008C22.6947 12.7682 22.8617 13.1999 23.2265 13.3643L25.0406 14.1686C25.0968 14.7356 25.1037 15.3065 25.0611 15.8747H24.0513C23.9511 15.8747 23.91 15.9415 23.91 16.0391V16.5016C23.91 17.5911 23.2959 17.83 22.7563 17.8891C22.2424 17.9482 21.6746 17.6733 21.6026 17.3598C21.2994 15.6563 20.7958 15.294 19.9993 14.6645C20.9885 14.0375 22.0163 13.11 22.0163 11.8715C22.0163 10.5328 21.099 9.69002 20.4747 9.27634C19.5959 8.69822 18.6247 8.5826 18.3626 8.5826H7.93063C9.37312 6.97114 11.3066 5.88021 13.4318 5.47871L14.6626 6.76857C14.9401 7.05891 15.4 7.07176 15.6903 6.79169L17.0676 5.47871ZM5.71063 11.3936C5.91853 11.3996 6.11589 11.4864 6.26079 11.6356C6.40568 11.7849 6.48669 11.9847 6.4866 12.1927C6.4866 12.4046 6.40241 12.6079 6.25255 12.7577C6.10269 12.9076 5.89944 12.9918 5.6875 12.9918C5.47557 12.9918 5.27232 12.9076 5.12246 12.7577C4.9726 12.6079 4.88841 12.4046 4.88841 12.1927C4.88836 12.0858 4.90977 11.9799 4.95137 11.8814C4.99297 11.7829 5.0539 11.6938 5.13057 11.6193C5.20724 11.5448 5.29809 11.4864 5.39772 11.4476C5.49736 11.4089 5.60376 11.3905 5.71063 11.3936ZM24.7631 11.4295C24.971 11.4356 25.1683 11.5224 25.3132 11.6716C25.4581 11.8208 25.5391 12.0206 25.539 12.2286C25.539 12.4406 25.4548 12.6438 25.305 12.7937C25.1551 12.9435 24.9519 13.0277 24.7399 13.0277C24.528 13.0277 24.3247 12.9435 24.1749 12.7937C24.025 12.6438 23.9408 12.4406 23.9408 12.2286C23.9408 12.1217 23.9622 12.0159 24.0038 11.9174C24.0454 11.8189 24.1063 11.7298 24.183 11.6553C24.2597 11.5807 24.3505 11.5224 24.4502 11.4836C24.5498 11.4448 24.6562 11.4264 24.7631 11.4295ZM7.21632 11.558H8.60896V17.8377H5.79799C5.45083 16.6186 5.34264 15.3439 5.47938 14.0838L7.20091 13.3181C7.56834 13.1536 7.73535 12.7245 7.57091 12.3571L7.21632 11.558ZM13.0284 11.6248H16.3456C16.5177 11.6248 17.5558 11.8227 17.5558 12.6012C17.5558 13.2461 16.7592 13.4774 16.104 13.4774H13.0258L13.0284 11.6248ZM13.0284 16.1368H15.5696C15.8008 16.1368 16.8106 16.2036 17.1318 17.4934C17.232 17.8891 17.4556 19.179 17.6072 19.5927C17.7588 20.0552 18.3729 20.9802 19.0281 20.9802H23.1751C22.8972 21.3524 22.5933 21.7044 22.2656 22.0336L20.5774 21.6713C20.1843 21.5866 19.7963 21.8384 19.7115 22.2315L19.3107 24.102C18.0217 24.6859 16.622 24.9848 15.2069 24.9781C13.7919 24.9714 12.395 24.6595 11.1116 24.0635L10.7108 22.1929C10.626 21.7998 10.2406 21.548 9.84743 21.6328L8.19528 21.9874C7.88869 21.6711 7.6037 21.3346 7.34223 20.9802H15.3769C15.4668 20.9802 15.5285 20.9647 15.5285 20.88V18.0381C15.5285 17.9559 15.4668 17.9379 15.3769 17.9379H13.0258L13.0284 16.1368ZM9.3207 22.6452C9.5286 22.6512 9.72596 22.738 9.87085 22.8872C10.0157 23.0364 10.0968 23.2363 10.0967 23.4443C10.0967 23.6562 10.0125 23.8594 9.86262 24.0093C9.71276 24.1592 9.50951 24.2434 9.29757 24.2434C9.08564 24.2434 8.88239 24.1592 8.73253 24.0093C8.58267 23.8594 8.49848 23.6562 8.49848 23.4443C8.49843 23.3373 8.51984 23.2315 8.56144 23.133C8.60303 23.0345 8.66397 22.9454 8.74064 22.8709C8.81731 22.7964 8.90815 22.738 9.00779 22.6992C9.10743 22.6605 9.21383 22.6421 9.3207 22.6452ZM21.153 22.6811C21.3609 22.6872 21.5583 22.774 21.7031 22.9232C21.848 23.0724 21.929 23.2722 21.929 23.4802C21.929 23.6922 21.8448 23.8954 21.6949 24.0453C21.5451 24.1951 21.3418 24.2793 21.1299 24.2793C20.9179 24.2793 20.7147 24.1951 20.5648 24.0453C20.415 23.8954 20.3308 23.6922 20.3308 23.4802C20.3307 23.3733 20.3521 23.2675 20.3937 23.169C20.4353 23.0705 20.4963 22.9814 20.5729 22.9068C20.6496 22.8323 20.7404 22.774 20.8401 22.7352C20.9397 22.6964 21.0461 22.678 21.153 22.6811Z" fill="black">
</path>
<path clip-rule="evenodd" d="M26.7232 15.2304C26.7232 18.2717 25.515 21.1885 23.3645 23.3391C21.2139 25.4896 18.2971 26.6978 15.2558 26.6978C12.2144 26.6978 9.29761 25.4896 7.14705 23.3391C4.99649 21.1885 3.78832 18.2717 3.78832 15.2304C3.78832 12.189 4.99649 9.27222 7.14705 7.12166C9.29761 4.9711 12.2144 3.76293 15.2558 3.76293C18.2971 3.76293 21.2139 4.9711 23.3645 7.12166C25.515 9.27222 26.7232 12.189 26.7232 15.2304ZM26.5073 14.1229L28.2957 15.2304L26.5073 16.3378L28.0439 17.7741L26.0757 18.5115L27.3039 20.2202L25.2252 20.5594L26.0988 22.4762L23.9944 22.4017L24.4775 24.4521L22.4271 23.969L22.5016 26.0734L20.5848 25.1998L20.2456 27.2785L18.5369 26.0503L17.7995 28.0185L16.3632 26.482L15.2558 28.2703L14.1483 26.482L12.712 28.0185L11.9746 26.0503L10.2659 27.2785L9.92672 25.1998L8.00992 26.0734L8.08443 23.969L6.03401 24.4521L6.51707 22.4017L4.4127 22.4762L5.28631 20.5594L3.20763 20.2202L4.43582 18.5115L2.46763 17.7741L4.00415 16.3378L2.21582 15.2304L4.00415 14.1229L2.46763 12.6866L4.43582 11.9492L3.20763 10.2405L5.28631 9.90133L4.4127 7.98453L6.51707 8.05904L6.03401 6.00862L8.08443 6.49168L8.00992 4.3873L9.92672 5.26092L10.2659 3.18223L11.9746 4.41043L12.712 2.44224L14.1483 3.97876L15.2558 2.19043L16.3632 3.97876L17.7995 2.44224L18.5369 4.41043L20.2456 3.18223L20.5848 5.26092L22.5016 4.3873L22.4271 6.49168L24.4775 6.00862L23.9944 8.05904L26.0988 7.98453L25.2252 9.90133L27.3039 10.2405L26.0757 11.9492L28.0439 12.6866L26.5073 14.1229Z" fill="black" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round">
</path>
</g>
<defs>
<clippath id="clip0_5454_14623">
<rect fill="black" height="30" width="30">
</rect>
</clippath>
</defs>
</svg>
<span>
Rust
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.mongodb.com/docs/languages/scala/scala-driver/current/tutorials/aggregation/#explain-an-aggregation" target="_self">
<svg class="leafygreen-ui-x3zji3" height="24" viewbox="0 0 70 70" width="24" xmlns="http://www.w3.org/2000/svg">
<defs>
<lineargradient id=":R3pt5qcmqn9:aaa" x1=".006%" x2="96.837%" y1="49.998%" y2="49.998%">
<stop offset="0%" stop-color="#CD413F">
</stop>
<stop offset="75.091%" stop-color="#CD4543">
</stop>
<stop offset="100%" stop-color="#D04543">
</stop>
</lineargradient>
</defs>
<g fill="none" fill-rule="nonzero">
<path d="M13 39v5c0 .843 17.73 2.255 29.28 5 5.582-1.327 9.72-2.966 9.72-5v-5c0-2.034-4.138-3.673-9.72-5C30.73 36.745 13 38.157 13 39M13 20v5c0 .843 17.73 2.255 29.28 5 5.582-1.327 9.72-2.966 9.72-5v-5c0-2.034-4.138-3.673-9.72-5C30.73 17.745 13 19.157 13 20" fill="#731818">
</path>
<g fill="url(#:R3pt5qcmqn9:aaa)" transform="translate(13)">
<path d="M0 29.53v14.766c0-1.232 39-3.692 39-9.844V19.686c0 6.152-39 8.612-39 9.844M0 9.843V24.61c0-1.231 39-3.691 39-9.843V0C39 6.152 0 8.612 0 9.843M0 49.216v14.766c0-1.231 39-3.691 39-9.843V39.373c0 6.151-39 8.612-39 9.843">
</path>
</g>
</g>
</svg>
<span>
Scala
</span>
</a>
</div>
<button class="leafygreen-ui-ki2t97">
Show all
<svg aria-label="Chevron Down Icon" class="" fill="none" height="16" role="img" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" d="M1.636 5.364a1 1 0 0 0 0 1.414l4.95 4.95.707.707a1 1 0 0 0 1.414 0l.707-.707 4.95-4.95a1 1 0 0 0 0-1.414l-.707-.707a1 1 0 0 0-1.414 0L8 8.899 3.757 4.657a1 1 0 0 0-1.414 0l-.707.707Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</button>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.collection.explain" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-db.collection.explain">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns information on the query plan for the following methods:
</p>
<table class="hlist">
<tbody>
<tr>
<td>
<ul class="simple">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.count/#mongodb-method-db.collection.count">
<code class="leafygreen-ui-1l06pbn">
count()
</code>
</a>
</p>
</li>
</ul>
</td>
<td>
<ul class="simple">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.remove/#mongodb-method-db.collection.remove">
<code class="leafygreen-ui-1l06pbn">
remove()
</code>
</a>
</p>
</li>
</ul>
</td>
<td>
<ul class="simple">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.distinct/#mongodb-method-db.collection.distinct">
<code class="leafygreen-ui-1l06pbn">
distinct()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
<code class="leafygreen-ui-1l06pbn">
findAndModify()
</code>
</a>
</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p class="leafygreen-ui-1kp3ins">
Returns information on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.mapReduce/#mongodb-method-db.collection.mapReduce">
<code class="leafygreen-ui-1l06pbn">
mapReduce()
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
, append one of the
aforementioned methods to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.explain().<method(...)>"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.collection.explain().<method(...)></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For example,
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.explain().remove( { category: \"apparel\" }, { justOne: true } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">remove</span>( <!-- -->{ <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"apparel"</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">justOne</span>: <span class="lg-highlight-literal">true</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more examples, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-explain-method-examples" target="_self">
<span>
Examples
</span>
</a>
. See also
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-explain-method-help" target="_self">
<span>
db.collection.explain().help()
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
method has the following
parameter:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
verbosity
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies the verbosity mode for the explain output. The mode affects
the behavior of
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
and determines the amount of information
to return. The possible modes are:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"queryPlanner"
</code>
(Default)
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"executionStats"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"allPlansExecution"
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For backwards compatibility with earlier versions of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.explain/#mongodb-method-cursor.explain">
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</a>
, MongoDB interprets
<code class="leafygreen-ui-1l06pbn">
true
</code>
as
<code class="leafygreen-ui-1l06pbn">
"allPlansExecution"
</code>
and
<code class="leafygreen-ui-1l06pbn">
false
</code>
as
<code class="leafygreen-ui-1l06pbn">
"queryPlanner"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on the modes, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-explain-method-verbosity" target="_self">
<span>
Verbosity Modes
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This method is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is supported in all MongoDB Atlas clusters.
For information on Atlas support for all commands, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-method-behavior">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Using
<code class="leafygreen-ui-1l06pbn">
explain
</code>
ignores all existing plan cache entries and prevents
the MongoDB query planner from creating a new plan cache entry.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-method-verbosity">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-method-queryPlanner">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-method-executionStats">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-method-allPlansExecution">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Verbosity Modes
<a class="headerlink leafygreen-ui-1kepx47" href="#verbosity-modes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="verbosity-modes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The behavior of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
and the amount of
information returned depend on the
<code class="leafygreen-ui-1l06pbn">
verbosity
</code>
mode.
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-9">
<div aria-label="Tabs to describe usage of allplans/executionstats/queryplanner" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_49khdxk4y" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_cu3n28gok" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
queryPlanner Mode (Default)
</div>
</button>
<button aria-controls="_mmwzk91p5" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_thmno05wh" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
executionStats Mode
</div>
</button>
<button aria-controls="_57nh0n9oh" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_1iljpi3zb" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
allPlansExecution Mode
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_udyi8op56" class="" id="_5iqv3ynfy" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
By default,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
runs in
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
verbosity mode.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB runs the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-read-operations-query-optimization">
query optimizer
</a>
to choose
the winning plan for the operation under evaluation.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
returns
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
information for the evaluated
<!-- -->
method
<!-- -->
.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_sruzmls40" class="leafygreen-ui-38lglc" id="_0249qdkku" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
MongoDB runs the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-read-operations-query-optimization">
query optimizer
</a>
to choose
the winning plan, executes the winning plan to completion, and returns
statistics describing the execution of the winning plan.
</p>
<p class="leafygreen-ui-1kp3ins">
For write operations,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
returns information about
the update or delete operations that
<em>
would
</em>
be performed, but does
<em>
not
</em>
apply the modifications to the database.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
returns the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
information for the evaluated
<!-- -->
method
<!-- -->
. However,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
does not
provide query execution information for the rejected plans.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_ddokqxw7a" class="leafygreen-ui-38lglc" id="_9htpcy66k" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
MongoDB runs the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/">
query optimizer
</a>
to choose the winning plan and executes the
winning plan to completion. In
<code class="leafygreen-ui-1l06pbn">
"allPlansExecution"
</code>
mode, MongoDB
returns statistics describing the execution of the winning plan as well
as statistics for the other candidate plans captured during
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
plan
selection
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For write operations,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
returns information about
the update or delete operations that
<em>
would
</em>
be performed, but does
<em>
not
</em>
apply the modifications to the database.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
returns the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
information for the evaluated
<!-- -->
method
<!-- -->
. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
includes the
<em>
completed
</em>
query execution information for the
<em>
winning plan
</em>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If the query optimizer considered more than one plan,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
information also includes the
<em>
partial
</em>
execution information captured during the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
plan selection phase
</a>
for both the winning and rejected
candidate plans.
</p>
</div>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Explain and Write Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#explain-and-write-operations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="explain-and-write-operations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For write operations,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
returns
information about the write operation that would be performed but does
not actually modify the database.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Restrictions
<a class="headerlink leafygreen-ui-1kepx47" href="#restrictions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="restrictions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You cannot run the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/explain/#mongodb-dbcommand-dbcmd.explain">
<code class="leafygreen-ui-1l06pbn">
explain
</code>
</a>
command/
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
in
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
mode
or
<code class="leafygreen-ui-1l06pbn">
allPlansExecution
</code>
mode for an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregation pipeline
</code>
</a>
that contains the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
stage.
Instead, you can either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
run the explain in
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
mode or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
run the explain in
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
mode or
<code class="leafygreen-ui-1l06pbn">
allPlansExecution
</code>
mode but without the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
stage to return information
for the stages that precede the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
stage.
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-method-mechanics">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
Mechanics
<a class="headerlink leafygreen-ui-1kepx47" href="#explain---mechanics" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="explain---mechanics">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
method wraps the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/explain/#mongodb-dbcommand-dbcmd.explain">
<code class="leafygreen-ui-1l06pbn">
explain
</code>
</a>
command and is the preferred way to run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/explain/#mongodb-dbcommand-dbcmd.explain">
<code class="leafygreen-ui-1l06pbn">
explain
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
</span>
</a>
is similar to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.explain/#mongodb-method-cursor.explain">
<code class="leafygreen-ui-1l06pbn">
db.collection.find().explain()
</code>
</a>
with the
following key differences:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
</span>
</a>
construct allows for the
additional chaining of query modifiers. For list of query modifiers,
see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-explain-method-help" target="_self">
<span>
db.collection.explain().find().help()
<!-- -->
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find().explain()
</code>
</span>
</a>
returns a cursor, which
requires a call to
<code class="leafygreen-ui-1l06pbn">
.next()
</code>
, or its alias
<code class="leafygreen-ui-1l06pbn">
.finish()
</code>
, to return
the
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
results.
If run interactively in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
automatically calls
<code class="leafygreen-ui-1l06pbn">
.finish()
</code>
to return
the results. For scripts, however, you must explicitly call
<code class="leafygreen-ui-1l06pbn">
.next()
</code>
, or
<code class="leafygreen-ui-1l06pbn">
.finish()
</code>
, to return the results. For list of
cursor-related methods, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-explain-method-help" target="_self">
<span>
db.collection.explain().find().help()
<!-- -->
.
</span>
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().aggregate()
</code>
</span>
</a>
is equivalent to passing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#std-label-example-aggregate-method-explain-option">
explain
</a>
option to
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
db.collection.aggregate()
</code>
</a>
method.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-method-help">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
help()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#help--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="help--">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To see the list of operations supported by
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
, run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.explain().help()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">help</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
</span>
</a>
returns a cursor, which allows for the chaining of query modifiers.
To see the list of query modifiers supported by
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
</span>
</a>
as
well as cursor-related methods, run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.explain().find().help()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">help</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You can chain multiple modifiers to
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
.
For an example, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-explain-find-modifiers" target="_self">
<span>
Explain
<code class="leafygreen-ui-1l06pbn">
find()
</code>
with Modifiers
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-collection-method-output">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Output
<a class="headerlink leafygreen-ui-1kepx47" href="#output" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="output">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
operations can return information regarding:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
explainVersion
</code>
, the output format version (for example,
<code class="leafygreen-ui-1l06pbn">
"1"
</code>
).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
command
</code>
, which details the command being explained.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
queryShapeHash
</code>
, starting in MongoDB 8.0, which is a hexadecimal
string with the hash of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-shape">
query shape
</a>
. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-shapes/#std-label-query-shapes">
Query Shapes
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-explain-output-query-shape-hash">
Query Shape Hash
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryShapeHash">
<code class="leafygreen-ui-1l06pbn">
explain.queryShapeHash
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
, which details the plan selected by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-read-operations-query-optimization">
query optimizer
</a>
and lists the rejected
plans.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
, which details the execution of the winning
plan and the rejected plans.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-serverInfo">
<code class="leafygreen-ui-1l06pbn">
serverInfo
</code>
</a>
, which provides information on the
MongoDB instance.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
serverParameters
</code>
, which details internal parameters.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The verbosity mode (i.e.
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
,
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
,
<code class="leafygreen-ui-1l06pbn">
allPlansExecution
</code>
) determines whether the results include
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
and whether
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
includes data
captured during
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
plan selection
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Explain output is limited by the maximum
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-Nested-Depth-for-BSON-Documents">
Nested Depth for BSON Documents
</a>
, which is 100 levels of nesting. Explain output that exceeds
the limit is truncated.
</p>
<p class="leafygreen-ui-1kp3ins">
For details on the output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/">
Explain Results
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-method-examples">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
Mode
<a class="headerlink leafygreen-ui-1kepx47" href="#queryplanner-mode" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="queryplanner-mode">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
By default,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
runs in
<code class="leafygreen-ui-1l06pbn">
"queryPlanner"
</code>
verbosity mode.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example runs
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
in
<code class="leafygreen-ui-1l06pbn">
"queryPlanner"
</code>
verbosity mode to return the query planning information
for the specified
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.count/#mongodb-method-db.collection.count">
<code class="leafygreen-ui-1l06pbn">
count()
</code>
</a>
operation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.explain().count( { quantity: { $gt: 50 } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">count</span>( <!-- -->{ <span class="lg-highlight-attr">quantity</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">50</span> } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
Mode
<a class="headerlink leafygreen-ui-1kepx47" href="#executionstats--mode" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="executionstats--mode">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example runs
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
in
<code class="leafygreen-ui-1l06pbn">
"executionStats"
</code>
verbosity mode
to return the query planning and execution information for the
specified
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</a>
operation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.explain(\"executionStats\").find(\n { quantity: { $gt: 50 }, category: \"apparel\" }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>(<span class="lg-highlight-string">"executionStats"</span>).<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">quantity</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">50</span> }<!-- -->, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"apparel"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
allPlansExecution
</code>
Mode
<a class="headerlink leafygreen-ui-1kepx47" href="#allplansexecution-mode" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="allplansexecution-mode">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example runs
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
in
<code class="leafygreen-ui-1l06pbn">
"allPlansExecution"
</code>
verbosity mode.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</span>
</a>
returns the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
for
all considered plans for the specified
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
<code class="leafygreen-ui-1l06pbn">
findAndModify()
</code>
</a>
operation:
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The execution of this explain will
<em>
not
</em>
modify data but runs the
query predicate of the update operation. For candidate plans,
MongoDB returns the execution information captured during the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
plan selection phase
<!-- -->
.
</a>
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.explain( \"allPlansExecution\" ).findAndModify( {\n query: { name: \"Tom\", state: \"active\", rating: { $gt: 10 } },\n sort: { rating: 1 },\n update: { $inc: { score: 1 } }\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>( <span class="lg-highlight-string">"allPlansExecution"</span> ).<span class="lg-highlight-title lg-highlight-function">findAndModify</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">query</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Tom"</span>, <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"active"</span>, <span class="lg-highlight-attr">rating</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">10</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sort</span>: <!-- -->{ <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">update</span>: <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">1</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-find-modifiers">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Explain
<code class="leafygreen-ui-1l06pbn">
find()
</code>
with Modifiers
<a class="headerlink leafygreen-ui-1kepx47" href="#explain-find---with-modifiers" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="explain-find---with-modifiers">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
</span>
</a>
construct allows for the chaining of query modifiers. For example, the
following operation provides information on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</a>
method with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#mongodb-method-cursor.sort">
<code class="leafygreen-ui-1l06pbn">
sort()
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hint/#mongodb-method-cursor.hint">
<code class="leafygreen-ui-1l06pbn">
hint()
</code>
</a>
query modifiers.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.explain(\"executionStats\").find(\n { quantity: { $gt: 50 }, category: \"apparel\" }\n).sort( { quantity: -1 } ).hint( { category: 1, quantity: -1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>(<span class="lg-highlight-string">"executionStats"</span>).<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">quantity</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">50</span> }<!-- -->, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"apparel"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">quantity</span>: <!-- -->-<span class="lg-highlight-number">1</span> } ).<span class="lg-highlight-title lg-highlight-function">hint</span>( <!-- -->{ <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">quantity</span>: <!-- -->-<span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For a list of query modifiers available, run the following in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.explain().find().help()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">help</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Iterate the
<code class="leafygreen-ui-1l06pbn">
explain().find()
</code>
Return Cursor
<a class="headerlink leafygreen-ui-1kepx47" href="#iterate-the-explain--.find---return-cursor" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="iterate-the-explain--.find---return-cursor">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
</span>
</a>
returns a cursor to the explain results. If run interactively in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
automatically
iterates the cursor using the
<code class="leafygreen-ui-1l06pbn">
.next()
</code>
method. For scripts, however,
you must explicitly call
<code class="leafygreen-ui-1l06pbn">
.next()
</code>
(or its alias
<code class="leafygreen-ui-1l06pbn">
.finish()
</code>
) to
return the results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var explainResult = db.products.explain().find( { category: \"apparel\" } ).next();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> explainResult <!-- -->= <!-- -->db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">explain</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"apparel"</span> } ).<span class="lg-highlight-title lg-highlight-function">next</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/administration/upgrade-community-to-enterprise/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Upgrade MongoDB Community to MongoDB Enterprise
<a class="headerlink leafygreen-ui-1kepx47" href="#upgrade-mongodb-community-to-mongodb-enterprise" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="upgrade-mongodb-community-to-mongodb-enterprise">
</div>
</a>
</h1>
<p class="leafygreen-ui-1kp3ins">
MongoDB Enterprise provides various features not available in the
MongoDB Community edition, such as:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/inmemory/">
In-Memory Storage Engine for Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/auditing/">
Auditing Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/kerberos/">
Kerberos Authentication on Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/">
Self-Managed LDAP Proxy Authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap-external/">
LDAP Authorization on Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-encryption-at-rest/">
Encryption at Rest
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The documents in this section provide instructions to upgrade from
MongoDB Community Edition to MongoDB Enterprise Edition.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Do not use these instructions to upgrade to another release version.
To upgrade release versions, refer to the appropriate release
upgrade instructions, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/8.0/#std-label-8.0-upgrade">
Upgrade to MongoDB 8.0
<!-- -->
.
</a>
</p>
</div>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If you use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
, the fully
managed service for MongoDB deployments in the cloud, you can
easily
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/tutorial/major-version-change/" target="_self">
<span>
upgrade your MongoDB version
</span>
</a>
. To migrate from
MongoDB Community Edition to MongoDB Atlas, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/import/" target="_self">
<span>
Migrate or Import Data
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Deployment
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Tutorial
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Standalone
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/upgrade-to-enterprise-standalone/">
Upgrade to MongoDB Enterprise (Standalone)
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Replica Set
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/upgrade-to-enterprise-replica-set/">
Upgrade to MongoDB Enterprise (Replica Set)
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Sharded Cluster
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/upgrade-to-enterprise-sharded-cluster/">
Upgrade to MongoDB Enterprise (Sharded Cluster)
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/implement-field-level-redaction/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Implement Field Level Redaction
<a class="headerlink leafygreen-ui-1kepx47" href="#implement-field-level-redaction" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="implement-field-level-redaction">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
pipeline operator restricts the contents of the
documents based on information stored in the documents themselves.
</p>
<div class="figure css-5vkxs0" style="width:700px">
<img alt="Diagram of security architecture with middleware and redaction." class="leafygreen-ui-1qnf57e" height="370" src="/docs/manual/images/redact-security-architecture.bakedsvg.svg" style="--border-color:#C1C7C6" width="520"/>
</div>
<p class="leafygreen-ui-1kp3ins">
To store the access criteria data, add a field to the documents and
embedded documents. To allow for multiple combinations of access levels for
the same data, consider setting the access field to an array of arrays.
Each array element contains a required set that allows a user with that
set to access the data.
</p>
<p class="leafygreen-ui-1kp3ins">
Then, include the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
stage in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
db.collection.aggregate()
</code>
</a>
operation to restrict contents of
the result set based on the access required to view the data.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
pipeline operator,
including its syntax and associated system variables as well as
additional examples, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
.
</a>
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For example, a
<code class="leafygreen-ui-1l06pbn">
forecasts
</code>
collection contains documents of the
following form where the
<code class="leafygreen-ui-1l06pbn">
tags
</code>
field determines the access levels
required to view the data:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 1,\n title: \"123 Department Report\",\n tags: [ [ \"G\" ], [ \"FDW\" ] ],\n year: 2014,\n subsections: [\n {\n subtitle: \"Section 1: Overview\",\n tags: [ [ \"SI\", \"G\" ], [ \"FDW\" ] ],\n content: \"Section 1: This is the content of section 1.\"\n },\n {\n subtitle: \"Section 2: Analysis\",\n tags: [ [ \"STLW\" ] ],\n content: \"Section 2: This is the content of section 2.\"\n },\n {\n subtitle: \"Section 3: Budgeting\",\n tags: [ [ \"TK\" ], [ \"FDW\", \"TGE\" ] ],\n content: {\n text: \"Section 3: This is the content of section3.\",\n tags: [ [ \"HCS\"], [ \"FDW\", \"TGE\", \"BX\" ] ]\n }\n }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">title</span>: <span class="lg-highlight-string">"123 Department Report"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"G"</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"FDW"</span> ] ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-number">2014</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subsections</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subtitle</span>: <span class="lg-highlight-string">"Section 1: Overview"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"SI"</span>, <span class="lg-highlight-string">"G"</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"FDW"</span> ] ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">content</span>: <span class="lg-highlight-string">"Section 1: This is the content of section 1."</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subtitle</span>: <span class="lg-highlight-string">"Section 2: Analysis"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"STLW"</span> ] ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">content</span>: <span class="lg-highlight-string">"Section 2: This is the content of section 2."</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subtitle</span>: <span class="lg-highlight-string">"Section 3: Budgeting"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"TK"</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"FDW"</span>, <span class="lg-highlight-string">"TGE"</span> ] ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">content</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">text</span>: <span class="lg-highlight-string">"Section 3: This is the content of section3."</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"HCS"</span>]<!-- -->, <!-- -->[ <span class="lg-highlight-string">"FDW"</span>, <span class="lg-highlight-string">"TGE"</span>, <span class="lg-highlight-string">"BX"</span> ] ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For each document, the
<code class="leafygreen-ui-1l06pbn">
tags
</code>
field contains various access groupings
necessary to view the data. For example, the value
<code class="leafygreen-ui-1l06pbn">
[ [ "G" ], [
"FDW", "TGE" ] ]
</code>
can specify that a user requires either access level
<code class="leafygreen-ui-1l06pbn">
["G"]
</code>
or both
<code class="leafygreen-ui-1l06pbn">
[ "FDW", "TGE" ]
</code>
to view the data.
</p>
<p class="leafygreen-ui-1kp3ins">
Consider a user who only has access to view information tagged with
either
<code class="leafygreen-ui-1l06pbn">
"FDW"
</code>
or
<code class="leafygreen-ui-1l06pbn">
"TGE"
</code>
. To run a query on all documents with
year
<code class="leafygreen-ui-1l06pbn">
2014
</code>
for this user, include a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
stage as in
the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var userAccess = [ \"FDW\", \"TGE\" ];\ndb.forecasts.aggregate(\n [\n { $match: { year: 2014 } },\n { $redact:\n {\n $cond: {\n if: { $anyElementTrue:\n {\n $map: {\n input: \"$tags\" ,\n as: \"fieldTag\",\n in: { $setIsSubset: [ \"$$fieldTag\", userAccess ] }\n }\n }\n },\n then: \"$$DESCEND\",\n else: \"$$PRUNE\"\n }\n }\n }\n ]\n)"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">var userAccess = [ "FDW", "TGE" ];</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.forecasts.aggregate(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { $match: { year: 2014 } },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { $redact:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $cond: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> if: { $anyElementTrue:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $map: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> input: "$tags" ,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> as: "fieldTag",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> in: { $setIsSubset: [ "$$fieldTag", userAccess ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> then: "$$DESCEND",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> else: "$$PRUNE"</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The aggregation operation returns the following "redacted" document for the user:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1,\n \"title\" : \"123 Department Report\",\n \"tags\" : [ [ \"G\" ], [ \"FDW\" ] ],\n \"year\" : 2014,\n \"subsections\" :\n [\n {\n \"subtitle\" : \"Section 1: Overview\",\n \"tags\" : [ [ \"SI\", \"G\" ], [ \"FDW\" ] ],\n \"content\" : \"Section 1: This is the content of section 1.\"\n },\n {\n \"subtitle\" : \"Section 3: Budgeting\",\n \"tags\" : [ [ \"TK\" ], [ \"FDW\", \"TGE\" ] ]\n }\n ]\n}"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ "_id" : 1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "title" : "123 Department Report",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "tags" : [ [ "G" ], [ "FDW" ] ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "year" : 2014,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "subsections" :</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "subtitle" : "Section 1: Overview",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "tags" : [ [ "SI", "G" ], [ "FDW" ] ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "content" : "Section 1: This is the content of section 1."</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "subtitle" : "Section 3: Budgeting",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "tags" : [ [ "TK" ], [ "FDW", "TGE" ] ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/map/#mongodb-expression-exp.-map">
<code class="leafygreen-ui-1l06pbn">
$map
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setIsSubset/#mongodb-expression-exp.-setIsSubset">
<code class="leafygreen-ui-1l06pbn">
$setIsSubset
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/anyElementTrue/#mongodb-expression-exp.-anyElementTrue">
<code class="leafygreen-ui-1l06pbn">
$anyElementTrue
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/sin/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$sin (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-sin--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-sin--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-sin" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-sin">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the sine of a value that is measured in radians.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $sin: <expression> }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$sin</span>: <<!-- -->expression<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
takes any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a number. If the
expression returns a value in degrees, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/degreesToRadians/#mongodb-expression-exp.-degreesToRadians">
<code class="leafygreen-ui-1l06pbn">
$degreesToRadians
</code>
</a>
operator to convert the
result to radians.
</p>
<p class="leafygreen-ui-1kp3ins">
By default
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
returns values as a
<code class="leafygreen-ui-1l06pbn">
double
</code>
.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
can also return values as a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimal
</span>
</a>
as long as the
<code class="leafygreen-ui-1l06pbn">
<expression>
</code>
resolves to a 128-bit decimal value.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
null
</code>
,
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
, and
<code class="leafygreen-ui-1l06pbn">
+/- Infinity
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#null--nan--and-----infinity" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="null--nan--and-----infinity">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the argument resolves to a value of
<code class="leafygreen-ui-1l06pbn">
null
</code>
or refers to a field
that is missing,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
. If the
argument resolves to
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
.
If the argument resolves to negative or positive infinity,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
throws an error.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:85%"/>
<col style="width:15%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Results
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $sin: NaN }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $sin: null }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
null
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ $sin : Infinity}
</code>
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
or
</em>
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ $sin : -Infinity }
</code>
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Throws an error message resembling the following formatted
output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"errmsg\" :\n \"Failed to optimize pipeline :: caused by :: cannot\n apply $sin to -inf, value must in (-inf,inf)\"","programmingLanguage":"Bash"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">"errmsg"</span> :</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Failed to optimize pipeline :: caused by :: cannot</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> apply </span><span class="lg-highlight-variable lg-highlight-string">$sin</span><span class="lg-highlight-string"> to -inf, value must in (-inf,inf)"</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-9">
<div aria-label="Tabs to describe usage of degrees/radians" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_ub17mkyvs" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_u2iwjn90z" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Sine of Value in Degrees
</div>
</button>
<button aria-controls="_tl2ia2kce" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_hruy63pwy" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Sine of Value in Radians
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_tvuytktzv" class="" id="_67d1780zm" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
trigonometry
</code>
collection contains a document that
stores the hypotenuse and one angle in a right-angle triangle:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5c50782193f833234ba90d85\"),\n \"angle_a\" : NumberDecimal(\"53.13010235415597870314438744090659\"),\n \"hypotenuse\" : NumberDecimal(\"5\")\n}","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> : ObjectId(<span class="lg-highlight-string">"5c50782193f833234ba90d85"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle_a"</span> : NumberDecimal(<span class="lg-highlight-string">"53.13010235415597870314438744090659"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hypotenuse"</span> : NumberDecimal(<span class="lg-highlight-string">"5"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
expression to calculate the side opposite
to
<code class="leafygreen-ui-1l06pbn">
angle_a
</code>
and add it to the input document using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
pipeline stage.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.aggregate([\n {\n $addFields : {\n \"side_b\" : {\n $multiply : [\n { $sin : {$degreesToRadians : \"$angle_a\"} },\n \"$hypotenuse\"\n ]\n }\n }\n }\n])","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.trigonometry.aggregate([</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-variable">$addFields</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"side_b"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-variable">$multiply</span> : [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { <span class="lg-highlight-variable">$sin</span> : {<span class="lg-highlight-variable">$degreesToRadians</span> : <span class="lg-highlight-string">"</span><span class="lg-highlight-variable lg-highlight-string">$angle_a</span><span class="lg-highlight-string">"</span>} },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"</span><span class="lg-highlight-variable lg-highlight-string">$hypotenuse</span><span class="lg-highlight-string">"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/degreesToRadians/#mongodb-expression-exp.-degreesToRadians">
<code class="leafygreen-ui-1l06pbn">
$degreesToRadians
</code>
</a>
expression converts the
degree value of
<code class="leafygreen-ui-1l06pbn">
angle_a
</code>
to the equivalent value in radians.
</p>
<p class="leafygreen-ui-1kp3ins">
The command returns the following output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5c50782193f833234ba90d85\"),\n \"angle_a\" : NumberDecimal(\"53.13010235415597870314438744090659\"),\n \"side_b\" : NumberDecimal(\"4.000000000000000000000000000000000\"),\n \"hypotenuse\" : NumberDecimal(\"5\"),\n}","programmingLanguage":"Bash"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> : ObjectId(<span class="lg-highlight-string">"5c50782193f833234ba90d85"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle_a"</span> : NumberDecimal(<span class="lg-highlight-string">"53.13010235415597870314438744090659"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"side_b"</span> : NumberDecimal(<span class="lg-highlight-string">"4.000000000000000000000000000000000"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hypotenuse"</span> : NumberDecimal(<span class="lg-highlight-string">"5"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Since
<code class="leafygreen-ui-1l06pbn">
angle_a
</code>
and
<code class="leafygreen-ui-1l06pbn">
hypotenuse
</code>
are stored as
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimals
</span>
</a>
, the output of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
is a 128-bit decimal.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_5z1n14urj" class="leafygreen-ui-38lglc" id="_ecjkor5v9" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
trigonometry
</code>
collection contains a document that
stores the hypotenuse and one angle in a right-angle triangle:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5c50782193f833234ba90d85\"),\n \"angle_a\" : NumberDecimal(\"0.9272952180016122324285124629224288\"),\n \"hypotenuse\" : NumberDecimal(\"5\")\n}","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> : ObjectId(<span class="lg-highlight-string">"5c50782193f833234ba90d85"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle_a"</span> : NumberDecimal(<span class="lg-highlight-string">"0.9272952180016122324285124629224288"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hypotenuse"</span> : NumberDecimal(<span class="lg-highlight-string">"5"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
expression to calculate the side opposite
to
<code class="leafygreen-ui-1l06pbn">
angle_a
</code>
and add it to the input document using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
pipeline stage.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.aggregate([\n {\n $addFields : {\n \"side_b\" : {\n $multiply : [\n { $sin : \"$angle_a\" },\n \"$hypotenuse\"\n ]\n }\n }\n }\n])","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.trigonometry.aggregate([</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-variable">$addFields</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"side_b"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-variable">$multiply</span> : [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { <span class="lg-highlight-variable">$sin</span> : <span class="lg-highlight-string">"</span><span class="lg-highlight-variable lg-highlight-string">$angle_a</span><span class="lg-highlight-string">"</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"</span><span class="lg-highlight-variable lg-highlight-string">$hypotenuse</span><span class="lg-highlight-string">"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The command returns the following output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5c50782193f833234ba90d85\"),\n \"angle_a\" : NumberDecimal(\"0.9272952180016122324285124629224288\"),\n \"side_b\" : NumberDecimal(\"4.000000000000000000000000000000000\"),\n \"hypotenuse\" : NumberDecimal(\"5\"),\n}","programmingLanguage":"Bash"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> : ObjectId(<span class="lg-highlight-string">"5c50782193f833234ba90d85"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle_a"</span> : NumberDecimal(<span class="lg-highlight-string">"0.9272952180016122324285124629224288"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"side_b"</span> : NumberDecimal(<span class="lg-highlight-string">"4.000000000000000000000000000000000"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hypotenuse"</span> : NumberDecimal(<span class="lg-highlight-string">"5"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Since
<code class="leafygreen-ui-1l06pbn">
angle_a
</code>
and
<code class="leafygreen-ui-1l06pbn">
hypotenuse
</code>
are stored as
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimals
</span>
</a>
, the output of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-sin" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</span>
</a>
is a 128-bit decimal.
</p>
</div>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Mongo.getDBs/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Mongo.getDBs()
<a class="headerlink leafygreen-ui-1kepx47" href="#mongo.getdbs--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongo.getdbs--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
Mongo.getDBs()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Mongo.getDBs" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-Mongo.getDBs">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns information about all available databases.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Mongo.getDBs" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
Mongo.getDBs()
</code>
</span>
</a>
uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/listDatabases/#mongodb-dbcommand-dbcmd.listDatabases">
<code class="leafygreen-ui-1l06pbn">
listDatabases
</code>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Mongo.getDBs" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
Mongo.getDBs()
</code>
</span>
</a>
method doesn't take any parameters.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To list the available databases and metadata for the local MongoDB
instance:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getMongo().getDBs()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getMongo</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">getDBs</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getMongo/#mongodb-method-db.getMongo">
<code class="leafygreen-ui-1l06pbn">
db.getMongo()
</code>
</a>
method returns the connection to the
current MongoDB instance. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Mongo.getDBs" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
Mongo.getDBs()
</code>
</span>
</a>
output
resembles:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n databases: [\n { name: 'admin', sizeOnDisk: Long(\"225280\"), empty: false },\n { name: 'config', sizeOnDisk: Long(\"212992\"), empty: false },\n { name: 'local', sizeOnDisk: Long(\"2400256\"), empty: false },\n { name: 'test', sizeOnDisk: Long(\"303104\"), empty: false }\n ],\n totalSize: Long(\"3141632\"),\n totalSizeMb: Long(\"2\"),\n ok: 1,\n '$clusterTime': {\n clusterTime: Timestamp({ t: 1640186473, i: 1 }),\n signature: {\n hash: Binary(Buffer.from(\"0000000000000000000000000000000000000000\", \"hex\"), 0),\n keyId: Long(\"0\")\n }\n },\n operationTime: Timestamp({ t: 1640186473, i: 1 })\n }","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">databases</span>: <!-- -->[</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'admin'</span>, <span class="lg-highlight-attr">sizeOnDisk</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"225280"</span>)<!-- -->, <span class="lg-highlight-attr">empty</span>: <span class="lg-highlight-literal">false</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'config'</span>, <span class="lg-highlight-attr">sizeOnDisk</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"212992"</span>)<!-- -->, <span class="lg-highlight-attr">empty</span>: <span class="lg-highlight-literal">false</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'local'</span>, <span class="lg-highlight-attr">sizeOnDisk</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"2400256"</span>)<!-- -->, <span class="lg-highlight-attr">empty</span>: <span class="lg-highlight-literal">false</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'test'</span>, <span class="lg-highlight-attr">sizeOnDisk</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"303104"</span>)<!-- -->, <span class="lg-highlight-attr">empty</span>: <span class="lg-highlight-literal">false</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalSize</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"3141632"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalSizeMb</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"2"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'$clusterTime'</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clusterTime</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<!-- -->{ <span class="lg-highlight-attr">t</span>: <span class="lg-highlight-number">1640186473</span>, <span class="lg-highlight-attr">i</span>: <span class="lg-highlight-number">1</span> })<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">signature</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hash</span>: <span class="lg-highlight-title lg-highlight-class">Binary</span>(<span class="lg-highlight-title lg-highlight-class">Buffer</span>.<span class="lg-highlight-title lg-highlight-function">from</span>(<span class="lg-highlight-string">"0000000000000000000000000000000000000000"</span>, <span class="lg-highlight-string">"hex"</span>)<!-- -->, <span class="lg-highlight-number">0</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyId</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"0"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">operationTime</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<!-- -->{ <span class="lg-highlight-attr">t</span>: <span class="lg-highlight-number">1640186473</span>, <span class="lg-highlight-attr">i</span>: <span class="lg-highlight-number">1</span> })</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The databases are listed in the highlighted lines.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Session.abortTransaction/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Session.abortTransaction()
<a class="headerlink leafygreen-ui-1kepx47" href="#session.aborttransaction--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="session.aborttransaction--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
Session.abortTransaction()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Session.abortTransaction" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-Session.abortTransaction">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Terminates the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
multi-document transaction
</a>
and rolls back any data changes made by the
operations within the transaction. That is, the transaction ends
without saving any of the changes made by the operations in the
transaction.
</p>
<p class="leafygreen-ui-1kp3ins">
Multi-document transactions are available for both sharded clusters and
replica sets.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
Session.abortTransaction()
</code>
does not return a value.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for database commands or language-specific drivers,
such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For the database command, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/abortTransaction/#mongodb-dbcommand-dbcmd.abortTransaction">
<code class="leafygreen-ui-1l06pbn">
abortTransaction
</code>
</a>
command
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This method is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is supported in all MongoDB Atlas clusters.
For information on Atlas support for all commands, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Atomicity
<a class="headerlink leafygreen-ui-1kepx47" href="#atomicity" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="atomicity">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When a transaction aborts, all data changes made by the writes in the
transaction are discarded without ever becoming visible and the
transaction ends.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Security
<a class="headerlink leafygreen-ui-1kepx47" href="#security" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="security">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If running with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/auditing/#std-label-auditing">
auditing
</a>
, operations in an
aborted transaction are still audited.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Retryable
<a class="headerlink leafygreen-ui-1kepx47" href="#retryable" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="retryable">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the abort operation encounters an error, MongoDB drivers retry the
abort operation a single time regardless of whether
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/connection-string-options/#mongodb-urioption-urioption.retryWrites">
<code class="leafygreen-ui-1l06pbn">
retryWrites
</code>
</a>
is set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
. For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions-in-applications/#std-label-transactions-retry">
Transaction Error Handling
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Consider a scenario where as changes are made to an employee's record
in the
<code class="leafygreen-ui-1l06pbn">
hr
</code>
database, you want to ensure that the
<code class="leafygreen-ui-1l06pbn">
events
</code>
collection in the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
database are in sync with the
<code class="leafygreen-ui-1l06pbn">
hr
</code>
changes and vice versa. That is, you want to ensure that these writes are done as a
single transaction, such that either both operations succeed or fail.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
employees
</code>
collection in the
<code class="leafygreen-ui-1l06pbn">
hr
</code>
database has the following
documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : ObjectId(\"5af0776263426f87dd69319a\"), \"employee\" : 3, \"name\" : { \"title\" : \"Mr.\", \"name\" : \"Iba Ochs\" }, \"status\" : \"Active\", \"department\" : \"ABC\" }\n{ \"_id\" : ObjectId(\"5af0776263426f87dd693198\"), \"employee\" : 1, \"name\" : { \"title\" : \"Miss\", \"name\" : \"Ann Thrope\" }, \"status\" : \"Active\", \"department\" : \"ABC\" }\n{ \"_id\" : ObjectId(\"5af0776263426f87dd693199\"), \"employee\" : 2, \"name\" : { \"title\" : \"Mrs.\", \"name\" : \"Eppie Delta\" }, \"status\" : \"Active\", \"department\" : \"XYZ\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5af0776263426f87dd69319a"</span>)<!-- -->, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Mr."</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Iba Ochs"</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"Active"</span>, <span class="lg-highlight-string">"department"</span> <!-- -->: <span class="lg-highlight-string">"ABC"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5af0776263426f87dd693198"</span>)<!-- -->, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Miss"</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Ann Thrope"</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"Active"</span>, <span class="lg-highlight-string">"department"</span> <!-- -->: <span class="lg-highlight-string">"ABC"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5af0776263426f87dd693199"</span>)<!-- -->, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Mrs."</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eppie Delta"</span> }<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"Active"</span>, <span class="lg-highlight-string">"department"</span> <!-- -->: <span class="lg-highlight-string">"XYZ"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
employees
</code>
collection has a unique index on the
<code class="leafygreen-ui-1l06pbn">
employee
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.employees.createIndex( { employee: 1 }, { unique: true } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">employees</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">unique</span>: <span class="lg-highlight-literal">true</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
events
</code>
collection in the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
database has the
following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : ObjectId(\"5af07daa051d92f02462644a\"), \"employee\" : 1, \"status\" : { \"new\" : \"Active\", \"old\" : null }, \"department\" : { \"new\" : \"ABC\", \"old\" : null } }\n{ \"_id\" : ObjectId(\"5af07daa051d92f02462644b\"), \"employee\" : 2, \"status\" : { \"new\" : \"Active\", \"old\" : null }, \"department\" : { \"new\" : \"XYZ\", \"old\" : null } }\n{ \"_id\" : ObjectId(\"5af07daa051d92f02462644c\"), \"employee\" : 3, \"status\" : { \"new\" : \"Active\", \"old\" : null }, \"department\" : { \"new\" : \"ABC\", \"old\" : null } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5af07daa051d92f02462644a"</span>)<!-- -->, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"new"</span> <!-- -->: <span class="lg-highlight-string">"Active"</span>, <span class="lg-highlight-string">"old"</span> <!-- -->: <span class="lg-highlight-literal">null</span> }<!-- -->, <span class="lg-highlight-string">"department"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"new"</span> <!-- -->: <span class="lg-highlight-string">"ABC"</span>, <span class="lg-highlight-string">"old"</span> <!-- -->: <span class="lg-highlight-literal">null</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5af07daa051d92f02462644b"</span>)<!-- -->, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"new"</span> <!-- -->: <span class="lg-highlight-string">"Active"</span>, <span class="lg-highlight-string">"old"</span> <!-- -->: <span class="lg-highlight-literal">null</span> }<!-- -->, <span class="lg-highlight-string">"department"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"new"</span> <!-- -->: <span class="lg-highlight-string">"XYZ"</span>, <span class="lg-highlight-string">"old"</span> <!-- -->: <span class="lg-highlight-literal">null</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5af07daa051d92f02462644c"</span>)<!-- -->, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"new"</span> <!-- -->: <span class="lg-highlight-string">"Active"</span>, <span class="lg-highlight-string">"old"</span> <!-- -->: <span class="lg-highlight-literal">null</span> }<!-- -->, <span class="lg-highlight-string">"department"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"new"</span> <!-- -->: <span class="lg-highlight-string">"ABC"</span>, <span class="lg-highlight-string">"old"</span> <!-- -->: <span class="lg-highlight-literal">null</span> } }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following example opens a transaction, attempts to add a record to
the
<code class="leafygreen-ui-1l06pbn">
events
</code>
collection and add a document to the
<code class="leafygreen-ui-1l06pbn">
employees
</code>
collection. If the operation encounters an error in either operations
or in committing the transaction, the session aborts the transaction.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// Runs the txnFunc and retries if TransientTransactionError encountered\n\nfunction runTransactionWithRetry(txnFunc, session) {\n while (true) {\n try {\n txnFunc(session); // performs transaction\n break;\n } catch (error) {\n // If transient error, retry the whole transaction\n if (error?.errorLabels?.includes(\"TransientTransactionError\")) {\n print(\"TransientTransactionError, retrying transaction ...\");\n continue;\n } else {\n throw error;\n }\n }\n }\n}\n\n// Retries commit if UnknownTransactionCommitResult encountered\n\nfunction commitWithRetry(session) {\n while (true) {\n try {\n session.commitTransaction(); // Uses write concern set at transaction start.\n print(\"Transaction committed.\");\n break;\n } catch (error) {\n // Can retry commit\n if (error?.errorLabels?.includes(\"UnknownTransactionCommitResult\") ) {\n print(\"UnknownTransactionCommitResult, retrying commit operation ...\");\n continue;\n } else {\n print(\"Error during commit ...\");\n throw error;\n }\n }\n }\n}\n\n// Performs inserts and count in a transaction\nfunction updateEmployeeInfo(session) {\n employeesCollection = session.getDatabase(\"hr\").employees;\n eventsCollection = session.getDatabase(\"reporting\").events;\n\n // Start a transaction for the session that uses:\n // - read concern \"snapshot\"\n // - write concern \"majority\"\n\n session.startTransaction( { readConcern: { level: \"snapshot\" }, writeConcern: { w: \"majority\" } } );\n\n try{\n eventsCollection.insertOne(\n { employee: 3, status: { new: \"Active\", old: null }, department: { new: \"XYZ\", old: null } }\n );\n\n // Count number of events for employee 3\n\n var countDoc = eventsCollection.aggregate( [ { $match: { employee: 3 } }, { $count: \"eventCounts\" } ] ).next();\n\n print( \"events count (in active transaction): \" + countDoc.eventCounts );\n\n // The following operations should fail as an employee ``3`` already exist in employees collection\n employeesCollection.insertOne(\n { employee: 3, name: { title: \"Miss\", name: \"Terri Bachs\" }, status: \"Active\", department: \"XYZ\" }\n );\n } catch (error) {\n print(\"Caught exception during transaction, aborting.\");\n session.abortTransaction();\n throw error;\n }\n\n commitWithRetry(session);\n\n} // End of updateEmployeeInfo function\n\n// Start a session.\nsession = db.getMongo().startSession( { readPreference: { mode: \"primary\" } } );\n\ntry{\n runTransactionWithRetry(updateEmployeeInfo, session);\n} catch (error) {\n // Do something with error\n} finally {\n session.endSession();\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Runs the txnFunc</span><span class="lg-highlight-comment"> and retries if </span><span class="lg-highlight-comment">TransientTransactionError encountered</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword lg-highlight-function">function</span> <span class="lg-highlight-title lg-highlight-function">runTransactionWithRetry</span>(<span class="lg-highlight-params">txnFunc, session</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">while</span> <!-- -->(<span class="lg-highlight-literal">true</span>) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">try</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">txnFunc</span>(<!-- -->session)<!-- -->; <span class="lg-highlight-comment">// performs transaction</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">break</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->} <span class="lg-highlight-keyword">catch</span> <!-- -->(<!-- -->error) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// If transient error,</span><span class="lg-highlight-comment"> retry the whole </span><span class="lg-highlight-comment">transaction</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> <!-- -->(<!-- -->error<!-- -->?<!-- -->.<span class="lg-highlight-property">errorLabels</span>?<!-- -->.<span class="lg-highlight-title lg-highlight-function">includes</span>(<span class="lg-highlight-string">"TransientTransactionError"</span>)) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(<span class="lg-highlight-string">"TransientTransactionError, retrying transaction ..."</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">continue</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->} <span class="lg-highlight-keyword">else</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">throw</span> <!-- -->error<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Retries commit if </span><span class="lg-highlight-comment">UnknownTransactionCommitResult encountered</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword lg-highlight-function">function</span> <span class="lg-highlight-title lg-highlight-function">commitWithRetry</span>(<span class="lg-highlight-params">session</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">while</span> <!-- -->(<span class="lg-highlight-literal">true</span>) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">try</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->session.<span class="lg-highlight-title lg-highlight-function">commitTransaction</span>(<!-- -->)<!-- -->; <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Uses write concern </span><span class="lg-highlight-comment">set at transaction start.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(<span class="lg-highlight-string">"Transaction committed."</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">break</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->} <span class="lg-highlight-keyword">catch</span> <!-- -->(<!-- -->error) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Can retry commit</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> <!-- -->(<!-- -->error<!-- -->?<!-- -->.<span class="lg-highlight-property">errorLabels</span>?<!-- -->.<span class="lg-highlight-title lg-highlight-function">includes</span>(<span class="lg-highlight-string">"UnknownTransactionCommitResult"</span>) ) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(<span class="lg-highlight-string">"UnknownTransactionCommitResult, retrying commit operation ..."</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">continue</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->} <span class="lg-highlight-keyword">else</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(<span class="lg-highlight-string">"Error during commit ..."</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">throw</span> <!-- -->error<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Performs inserts and </span><span class="lg-highlight-comment">count in a transaction</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword lg-highlight-function">function</span> <span class="lg-highlight-title lg-highlight-function">updateEmployeeInfo</span>(<span class="lg-highlight-params">session</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->employeesCollection <!-- -->= <!-- -->session.<span class="lg-highlight-title lg-highlight-function">getDatabase</span>(<span class="lg-highlight-string">"hr"</span>).<span class="lg-highlight-property">employees</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->eventsCollection <!-- -->= <!-- -->session.<span class="lg-highlight-title lg-highlight-function">getDatabase</span>(<span class="lg-highlight-string">"reporting"</span>).<span class="lg-highlight-property">events</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Start a transaction </span><span class="lg-highlight-comment">for</span><span class="lg-highlight-comment"> the session that </span><span class="lg-highlight-comment">uses:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// - read concern "snapshot"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// - write concern "majority"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->session.<span class="lg-highlight-title lg-highlight-function">startTransaction</span>( <!-- -->{ <span class="lg-highlight-attr">readConcern</span>: <!-- -->{ <span class="lg-highlight-attr">level</span>: <span class="lg-highlight-string">"snapshot"</span> }<!-- -->, <span class="lg-highlight-attr">writeConcern</span>: <!-- -->{ <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-string">"majority"</span> } } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">try</span>{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->eventsCollection.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">status</span>: <!-- -->{ <span class="lg-highlight-attr">new</span>: <span class="lg-highlight-string">"Active"</span>, <span class="lg-highlight-attr">old</span>: <span class="lg-highlight-literal">null</span> }<!-- -->, <span class="lg-highlight-attr">department</span>: <!-- -->{ <span class="lg-highlight-attr">new</span>: <span class="lg-highlight-string">"XYZ"</span>, <span class="lg-highlight-attr">old</span>: <span class="lg-highlight-literal">null</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Count number of</span><span class="lg-highlight-comment"> events for employee </span><span class="lg-highlight-comment">3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> countDoc <!-- -->= <!-- -->eventsCollection.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-number">3</span> } }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">$count</span>: <span class="lg-highlight-string">"eventCounts"</span> } ] ).<span class="lg-highlight-title lg-highlight-function">next</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>( <span class="lg-highlight-string">"events count (in active transaction): "</span> <!-- -->+ <!-- -->countDoc.<span class="lg-highlight-property">eventCounts</span> )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> The following operations </span><span class="lg-highlight-comment">should fail as an employee ``3``</span><span class="lg-highlight-comment"> already exist in </span><span class="lg-highlight-comment">employees collection</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->employeesCollection.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">name</span>: <!-- -->{ <span class="lg-highlight-attr">title</span>: <span class="lg-highlight-string">"Miss"</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Terri Bachs"</span> }<!-- -->, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"Active"</span>, <span class="lg-highlight-attr">department</span>: <span class="lg-highlight-string">"XYZ"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->} <span class="lg-highlight-keyword">catch</span> <!-- -->(<!-- -->error) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(<span class="lg-highlight-string">"Caught exception during transaction, aborting."</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->session.<span class="lg-highlight-title lg-highlight-function">abortTransaction</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">throw</span> <!-- -->error<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">commitWithRetry</span>(<!-- -->session)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">} <span class="lg-highlight-comment">// End of updateEmployeeInfo function</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Start a session.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">session <!-- -->= <!-- -->db.<span class="lg-highlight-title lg-highlight-function">getMongo</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">startSession</span>( <!-- -->{ <span class="lg-highlight-attr">readPreference</span>: <!-- -->{ <span class="lg-highlight-attr">mode</span>: <span class="lg-highlight-string">"primary"</span> } } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">try</span>{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">runTransactionWithRetry</span>(<!-- -->updateEmployeeInfo<!-- -->, <!-- -->session)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} <span class="lg-highlight-keyword">catch</span> <!-- -->(<!-- -->error) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Do something with error</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">} <span class="lg-highlight-keyword">finally</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->session.<span class="lg-highlight-title lg-highlight-function">endSession</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Session.startTransaction/#mongodb-method-Session.startTransaction">
<code class="leafygreen-ui-1l06pbn">
Session.startTransaction()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Session.commitTransaction/#mongodb-method-Session.commitTransaction">
<code class="leafygreen-ui-1l06pbn">
Session.commitTransaction()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Database Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#database-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="database-commands">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#command-syntax" target="_self">
<span>
Command Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#user-commands" target="_self">
<span>
User Commands
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#database-operations" target="_self">
<span>
Database Operations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#auditing-commands" target="_self">
<span>
Auditing Commands
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#atlas-search-commands" target="_self">
<span>
Atlas Search Commands
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
All command documentation outlined below describes a command and
its available parameters and provides a document template or prototype
for each command. Some command documentation also includes the relevant
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
helpers.
</p>
<p class="leafygreen-ui-1kp3ins">
To run a command against the current database, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.runCommand/#mongodb-method-db.runCommand">
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { <command> } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{ <<!-- -->command<!-- -->> <!-- -->} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To run an administrative command against the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.adminCommand/#mongodb-method-db.adminCommand">
<code class="leafygreen-ui-1l06pbn">
db.adminCommand()
</code>
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { <command> } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{ <<!-- -->command<!-- -->> <!-- -->} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
For details on a specific command, including syntax and examples,
click on the link to the command's reference page.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#command-syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When you run a database command, you specify the command as a document
to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.runCommand/#mongodb-method-db.runCommand">
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
</a>
. The document's key is the command to run,
and the value is typically supplied as
<code class="leafygreen-ui-1l06pbn">
1
</code>
. The value does not affect
the output of the command for example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { hello: 1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{ <span class="lg-highlight-attr">hello</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
User Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#user-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="user-commands">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Aggregation Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="aggregation-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Performs
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
aggregation tasks
</a>
such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
using an aggregation pipeline.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Limited support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#limited-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/count/#mongodb-dbcommand-dbcmd.count">
<code class="leafygreen-ui-1l06pbn">
count
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Counts the number of documents in a collection or a view.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Limited support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#limited-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/distinct/#mongodb-dbcommand-dbcmd.distinct">
<code class="leafygreen-ui-1l06pbn">
distinct
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Displays the distinct values found for a specified key in a collection or a view.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Limited support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#limited-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
<code class="leafygreen-ui-1l06pbn">
mapReduce
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Performs
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/#std-label-map-reduce">
map-reduce
</a>
aggregation for large data sets.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Geospatial Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#geospatial-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="geospatial-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/geoSearch/#mongodb-dbcommand-dbcmd.geoSearch">
<code class="leafygreen-ui-1l06pbn">
geoSearch
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<em>
Removed in MongoDB 5.0.
</em>
Performs a geospatial query that uses
MongoDB's haystack index functionality.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-collection-commands">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Query and Write Operation Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#query-and-write-operation-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="query-and-write-operation-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/bulkWrite/#mongodb-dbcommand-dbcmd.bulkWrite">
<code class="leafygreen-ui-1l06pbn">
bulkWrite
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Performs many insert, update, and delete operations on multiple
collections in one request.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/delete/#mongodb-dbcommand-dbcmd.delete">
<code class="leafygreen-ui-1l06pbn">
delete
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deletes one or more documents.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/find/#mongodb-dbcommand-dbcmd.find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Selects documents in a collection or a view.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Limited support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#limited-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/findAndModify/#mongodb-dbcommand-dbcmd.findAndModify">
<code class="leafygreen-ui-1l06pbn">
findAndModify
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns and modifies a single document.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns batches of documents currently pointed to by the cursor.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/insert/#mongodb-dbcommand-dbcmd.insert">
<code class="leafygreen-ui-1l06pbn">
insert
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Inserts one or more documents.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/resetError/#mongodb-dbcommand-dbcmd.resetError">
<code class="leafygreen-ui-1l06pbn">
resetError
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<em>
Removed in MongoDB 5.0.
</em>
Resets the last error status.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/update/#mongodb-dbcommand-dbcmd.update">
<code class="leafygreen-ui-1l06pbn">
update
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Updates one or more documents.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-query-plan-commands">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Query Plan Cache Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#query-plan-cache-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="query-plan-cache-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/planCacheClear/#mongodb-dbcommand-dbcmd.planCacheClear">
<code class="leafygreen-ui-1l06pbn">
planCacheClear
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes cached query plan(s) for a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/planCacheClearFilters/#mongodb-dbcommand-dbcmd.planCacheClearFilters">
<code class="leafygreen-ui-1l06pbn">
planCacheClearFilters
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Clears index filter(s) for a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/planCacheListFilters/#mongodb-dbcommand-dbcmd.planCacheListFilters">
<code class="leafygreen-ui-1l06pbn">
planCacheListFilters
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Lists the index filters for a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/planCacheSetFilter/#mongodb-dbcommand-dbcmd.planCacheSetFilter">
<code class="leafygreen-ui-1l06pbn">
planCacheSetFilter
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Sets an index filter for a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Database Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#database-operations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="database-operations">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Authentication Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#authentication-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="authentication-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/authenticate/#mongodb-dbcommand-dbcmd.authenticate">
<code class="leafygreen-ui-1l06pbn">
authenticate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Starts an authenticated session using a username and password.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/logout/#mongodb-dbcommand-dbcmd.logout">
<code class="leafygreen-ui-1l06pbn">
logout
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Terminates the current authenticated session.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-user-management-commands">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
User Management Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#user-management-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="user-management-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createUser/#mongodb-dbcommand-dbcmd.createUser">
<code class="leafygreen-ui-1l06pbn">
createUser
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Creates a new user.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropAllUsersFromDatabase/#mongodb-dbcommand-dbcmd.dropAllUsersFromDatabase">
<code class="leafygreen-ui-1l06pbn">
dropAllUsersFromDatabase
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deletes all users associated with a database.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropUser/#mongodb-dbcommand-dbcmd.dropUser">
<code class="leafygreen-ui-1l06pbn">
dropUser
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes a single user.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/grantRolesToUser/#mongodb-dbcommand-dbcmd.grantRolesToUser">
<code class="leafygreen-ui-1l06pbn">
grantRolesToUser
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Grants a role and its privileges to a user.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/revokeRolesFromUser/#mongodb-dbcommand-dbcmd.revokeRolesFromUser">
<code class="leafygreen-ui-1l06pbn">
revokeRolesFromUser
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes a role from a user.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/updateUser/#mongodb-dbcommand-dbcmd.updateUser">
<code class="leafygreen-ui-1l06pbn">
updateUser
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Updates a user's data.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/usersInfo/#mongodb-dbcommand-dbcmd.usersInfo">
<code class="leafygreen-ui-1l06pbn">
usersInfo
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information about the specified users.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Limited support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#limited-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-role-management-commands">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Role Management Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#role-management-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="role-management-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createRole/#mongodb-dbcommand-dbcmd.createRole">
<code class="leafygreen-ui-1l06pbn">
createRole
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Creates a role and specifies its privileges.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropRole/#mongodb-dbcommand-dbcmd.dropRole">
<code class="leafygreen-ui-1l06pbn">
dropRole
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deletes the user-defined role.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropAllRolesFromDatabase/#mongodb-dbcommand-dbcmd.dropAllRolesFromDatabase">
<code class="leafygreen-ui-1l06pbn">
dropAllRolesFromDatabase
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deletes all user-defined roles from a database.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/grantPrivilegesToRole/#mongodb-dbcommand-dbcmd.grantPrivilegesToRole">
<code class="leafygreen-ui-1l06pbn">
grantPrivilegesToRole
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Assigns privileges to a user-defined role.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/grantRolesToRole/#mongodb-dbcommand-dbcmd.grantRolesToRole">
<code class="leafygreen-ui-1l06pbn">
grantRolesToRole
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies roles from which a user-defined role inherits privileges.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/invalidateUserCache/#mongodb-dbcommand-dbcmd.invalidateUserCache">
<code class="leafygreen-ui-1l06pbn">
invalidateUserCache
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Flushes the in-memory cache of user information, including credentials and roles.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/revokePrivilegesFromRole/#mongodb-dbcommand-dbcmd.revokePrivilegesFromRole">
<code class="leafygreen-ui-1l06pbn">
revokePrivilegesFromRole
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes the specified privileges from a user-defined role.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/revokeRolesFromRole/#mongodb-dbcommand-dbcmd.revokeRolesFromRole">
<code class="leafygreen-ui-1l06pbn">
revokeRolesFromRole
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes specified inherited roles from a user-defined role.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/rolesInfo/#mongodb-dbcommand-dbcmd.rolesInfo">
<code class="leafygreen-ui-1l06pbn">
rolesInfo
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information for the specified role or roles.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/updateRole/#mongodb-dbcommand-dbcmd.updateRole">
<code class="leafygreen-ui-1l06pbn">
updateRole
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Updates a user-defined role.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Replication Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#replication-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="replication-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/appendOplogNote/#mongodb-dbcommand-dbcmd.appendOplogNote">
<code class="leafygreen-ui-1l06pbn">
appendOplogNote
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Writes a non-operational entry to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
<!-- -->
.
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/applyOps/#mongodb-dbcommand-dbcmd.applyOps">
<code class="leafygreen-ui-1l06pbn">
applyOps
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Internal command that applies
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
entries to the current data set.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/hello/#mongodb-dbcommand-dbcmd.hello">
<code class="leafygreen-ui-1l06pbn">
hello
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Displays information about this member's role in the replica set, including whether it is the primary.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetAbortPrimaryCatchUp/#mongodb-dbcommand-dbcmd.replSetAbortPrimaryCatchUp">
<code class="leafygreen-ui-1l06pbn">
replSetAbortPrimaryCatchUp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Forces the elected
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
to abort sync (catch up) then complete the transition to primary.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetFreeze/#mongodb-dbcommand-dbcmd.replSetFreeze">
<code class="leafygreen-ui-1l06pbn">
replSetFreeze
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Prevents the current member from seeking election as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
for a period of time.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetConfig/#mongodb-dbcommand-dbcmd.replSetGetConfig">
<code class="leafygreen-ui-1l06pbn">
replSetGetConfig
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the replica set's configuration object.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetStatus/#mongodb-dbcommand-dbcmd.replSetGetStatus">
<code class="leafygreen-ui-1l06pbn">
replSetGetStatus
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a document that reports on the status of the replica set.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetInitiate/#mongodb-dbcommand-dbcmd.replSetInitiate">
<code class="leafygreen-ui-1l06pbn">
replSetInitiate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Initializes a new replica set.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetMaintenance/#mongodb-dbcommand-dbcmd.replSetMaintenance">
<code class="leafygreen-ui-1l06pbn">
replSetMaintenance
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Enables or disables a maintenance mode, which puts a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
node in a
<code class="leafygreen-ui-1l06pbn">
RECOVERING
</code>
state.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetReconfig/#mongodb-dbcommand-dbcmd.replSetReconfig">
<code class="leafygreen-ui-1l06pbn">
replSetReconfig
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Applies a new configuration to an existing replica set.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetResizeOplog/#mongodb-dbcommand-dbcmd.replSetResizeOplog">
<code class="leafygreen-ui-1l06pbn">
replSetResizeOplog
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Dynamically resizes the oplog for a replica set member. Available for WiredTiger storage engine only.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetStepDown/#mongodb-dbcommand-dbcmd.replSetStepDown">
<code class="leafygreen-ui-1l06pbn">
replSetStepDown
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Forces the current
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
to
<em>
step down
</em>
and become a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
, forcing an election.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetSyncFrom/#mongodb-dbcommand-dbcmd.replSetSyncFrom">
<code class="leafygreen-ui-1l06pbn">
replSetSyncFrom
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Explicitly override the default logic for selecting a member to replicate from.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/replication/">
Replication
</a>
for more information regarding
replication.
</p>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sharding Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#sharding-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sharding-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/abortReshardCollection/#mongodb-dbcommand-dbcmd.abortReshardCollection">
<code class="leafygreen-ui-1l06pbn">
abortReshardCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Aborts a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
resharding operation
<!-- -->
.
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/abortMoveCollection/#mongodb-dbcommand-dbcmd.abortMoveCollection">
<code class="leafygreen-ui-1l06pbn">
abortMoveCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Stops an in-progress
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveCollection/#mongodb-dbcommand-dbcmd.moveCollection">
<code class="leafygreen-ui-1l06pbn">
moveCollection
</code>
</a>
operation.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
</span>
</a>
and Atlas
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/manage-clusters/" target="_self">
<span>
Shared tier
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/abortUnshardCollection/#mongodb-dbcommand-dbcmd.abortUnshardCollection">
<code class="leafygreen-ui-1l06pbn">
abortUnshardCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Stops an in-progress
<code class="leafygreen-ui-1l06pbn">
unshardCollection
</code>
operation.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
</span>
</a>
and Atlas
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/manage-clusters/" target="_self">
<span>
Shared tier
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/addShard/#mongodb-dbcommand-dbcmd.addShard">
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shard
</a>
to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded cluster
<!-- -->
.
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/addShardToZone/#mongodb-dbcommand-dbcmd.addShardToZone">
<code class="leafygreen-ui-1l06pbn">
addShardToZone
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Associates a shard with a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-zone">
zone
</a>
. Supports configuring
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zone-sharding">
zones
</a>
in sharded clusters.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/analyzeShardKey/#mongodb-dbcommand-dbcmd.analyzeShardKey">
<code class="leafygreen-ui-1l06pbn">
analyzeShardKey
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Calculates metrics for evaluating a shard key.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/balancerCollectionStatus/#mongodb-dbcommand-dbcmd.balancerCollectionStatus">
<code class="leafygreen-ui-1l06pbn">
balancerCollectionStatus
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information on whether the chunks of a sharded
collection are balanced.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/balancerStart/#mongodb-dbcommand-dbcmd.balancerStart">
<code class="leafygreen-ui-1l06pbn">
balancerStart
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Starts a balancer thread.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/balancerStatus/#mongodb-dbcommand-dbcmd.balancerStatus">
<code class="leafygreen-ui-1l06pbn">
balancerStatus
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information on the balancer status.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/balancerStop/#mongodb-dbcommand-dbcmd.balancerStop">
<code class="leafygreen-ui-1l06pbn">
balancerStop
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Stops the balancer thread.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/checkMetadataConsistency/#mongodb-dbcommand-dbcmd.checkMetadataConsistency">
<code class="leafygreen-ui-1l06pbn">
checkMetadataConsistency
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Performs a series of consistency checks on sharding metadata.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/clearJumboFlag/#mongodb-dbcommand-dbcmd.clearJumboFlag">
<code class="leafygreen-ui-1l06pbn">
clearJumboFlag
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Clears the
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
flag for a chunk.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/cleanupReshardCollection/#mongodb-dbcommand-dbcmd.cleanupReshardCollection">
<code class="leafygreen-ui-1l06pbn">
cleanupReshardCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Cleans up a failed
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
resharding operation
<!-- -->
.
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/commitReshardCollection/#mongodb-dbcommand-dbcmd.commitReshardCollection">
<code class="leafygreen-ui-1l06pbn">
commitReshardCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Forces a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
resharding operation
</a>
to
block writes and complete.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/configureCollectionBalancing/#mongodb-dbcommand-dbcmd.configureCollectionBalancing">
<code class="leafygreen-ui-1l06pbn">
configureCollectionBalancing
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Configures balancer settings on a sharded collection.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/configureQueryAnalyzer/#mongodb-dbcommand-dbcmd.configureQueryAnalyzer">
<code class="leafygreen-ui-1l06pbn">
configureQueryAnalyzer
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Configures query sampling for a collection.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/enableSharding/#mongodb-dbcommand-dbcmd.enableSharding">
<code class="leafygreen-ui-1l06pbn">
enableSharding
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Creates a database.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/flushRouterConfig/#mongodb-dbcommand-dbcmd.flushRouterConfig">
<code class="leafygreen-ui-1l06pbn">
flushRouterConfig
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Forces a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
/
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance to update its cached routing metadata.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getShardMap/#mongodb-dbcommand-dbcmd.getShardMap">
<code class="leafygreen-ui-1l06pbn">
getShardMap
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Internal command that reports on the state of a sharded cluster.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/isdbgrid/#mongodb-dbcommand-dbcmd.isdbgrid">
<code class="leafygreen-ui-1l06pbn">
isdbgrid
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Verifies that a process is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
.
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/listShards/#mongodb-dbcommand-dbcmd.listShards">
<code class="leafygreen-ui-1l06pbn">
listShards
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a list of configured shards.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveChunk/#mongodb-dbcommand-dbcmd.moveChunk">
<code class="leafygreen-ui-1l06pbn">
moveChunk
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Internal command that migrates chunks between shards.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/movePrimary/#mongodb-dbcommand-dbcmd.movePrimary">
<code class="leafygreen-ui-1l06pbn">
movePrimary
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reassigns the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary-shard">
primary shard
</a>
when removing a shard from a sharded cluster.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveRange/#mongodb-dbcommand-dbcmd.moveRange">
<code class="leafygreen-ui-1l06pbn">
moveRange
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Moves ranges between shards.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mergeAllChunksOnShard/#mongodb-dbcommand-dbcmd.mergeAllChunksOnShard">
<code class="leafygreen-ui-1l06pbn">
mergeAllChunksOnShard
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Merges all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mergeAllChunksOnShard/#std-label-mergeability">
mergeable chunks
</a>
that a shard
owns for a given collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mergeChunks/#mongodb-dbcommand-dbcmd.mergeChunks">
<code class="leafygreen-ui-1l06pbn">
mergeChunks
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Provides the ability to combine chunks on a single shard.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/refineCollectionShardKey/#mongodb-dbcommand-dbcmd.refineCollectionShardKey">
<code class="leafygreen-ui-1l06pbn">
refineCollectionShardKey
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Refines a collection's shard key by adding a suffix to the
existing key.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/removeShard/#mongodb-dbcommand-dbcmd.removeShard">
<code class="leafygreen-ui-1l06pbn">
removeShard
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Starts the process of removing a shard from a sharded cluster.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/removeShardFromZone/#mongodb-dbcommand-dbcmd.removeShardFromZone">
<code class="leafygreen-ui-1l06pbn">
removeShardFromZone
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes the association between a shard and a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-zone">
zone
</a>
. Supports configuring
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zone-sharding">
zones
</a>
in sharded clusters.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/reshardCollection/#mongodb-dbcommand-dbcmd.reshardCollection">
<code class="leafygreen-ui-1l06pbn">
reshardCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Initiates a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
resharding operation
</a>
to change the
shard key for a collection, changing the distribution of your data.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setAllowMigrations/#mongodb-dbcommand-dbcmd.setAllowMigrations">
<code class="leafygreen-ui-1l06pbn">
setAllowMigrations
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Prevents new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-sharding-chunk-migration-automatic">
automatic migrations
</a>
on a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#mongodb-dbcommand-dbcmd.shardCollection">
<code class="leafygreen-ui-1l06pbn">
shardCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Enables the sharding functionality for a collection, allowing the collection to be sharded.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardingState/#mongodb-dbcommand-dbcmd.shardingState">
<code class="leafygreen-ui-1l06pbn">
shardingState
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reports whether the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
is a member of a sharded cluster.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/split/#mongodb-dbcommand-dbcmd.split">
<code class="leafygreen-ui-1l06pbn">
split
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Creates a new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunk
<!-- -->
.
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/transitionFromDedicatedConfigServer/#mongodb-dbcommand-dbcmd.transitionFromDedicatedConfigServer">
<code class="leafygreen-ui-1l06pbn">
transitionFromDedicatedConfigServer
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Configures a dedicated config server to run as a config shard.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
</span>
</a>
and Atlas
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/manage-clusters/" target="_self">
<span>
Shared tier
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/transitionToDedicatedConfigServer/#mongodb-dbcommand-dbcmd.transitionToDedicatedConfigServer">
<code class="leafygreen-ui-1l06pbn">
transitionToDedicatedConfigServer
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Configures a config shard to run as a dedicated config server.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
</span>
</a>
and Atlas
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/manage-clusters/" target="_self">
<span>
Shared tier
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/unsetSharding/#mongodb-dbcommand-dbcmd.unsetSharding">
<code class="leafygreen-ui-1l06pbn">
unsetSharding
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<em>
Removed in MongoDB 5.0.
</em>
Internal command that affects
connections between instances in a MongoDB deployment.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/unshardCollection/#mongodb-dbcommand-dbcmd.unshardCollection">
<code class="leafygreen-ui-1l06pbn">
unshardCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Unshards an existing sharded collection and moves the collection
data onto a single shard. When you unshard a collection,
the collection cannot be partitioned across multiple
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shards
</a>
and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
shard key
</a>
is removed.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/updateZoneKeyRange/#mongodb-dbcommand-dbcmd.updateZoneKeyRange">
<code class="leafygreen-ui-1l06pbn">
updateZoneKeyRange
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds or removes the association between a range of sharded data and a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-zone">
zone
</a>
. Supports configuring
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zone-sharding">
zones
</a>
in sharded clusters.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/">
Sharding
</a>
for more information about MongoDB's
sharding functionality.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-session-commands">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Session Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#session-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="session-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Command
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/abortTransaction/#mongodb-dbcommand-dbcmd.abortTransaction">
<code class="leafygreen-ui-1l06pbn">
abortTransaction
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Abort transaction.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/commitTransaction/#mongodb-dbcommand-dbcmd.commitTransaction">
<code class="leafygreen-ui-1l06pbn">
commitTransaction
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Commit transaction.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/endSessions/#mongodb-dbcommand-dbcmd.endSessions">
<code class="leafygreen-ui-1l06pbn">
endSessions
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Expire sessions before the sessions' timeout period.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killAllSessions/#mongodb-dbcommand-dbcmd.killAllSessions">
<code class="leafygreen-ui-1l06pbn">
killAllSessions
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Kill all sessions.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killAllSessionsByPattern/#mongodb-dbcommand-dbcmd.killAllSessionsByPattern">
<code class="leafygreen-ui-1l06pbn">
killAllSessionsByPattern
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Kill all sessions that match the specified pattern
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killSessions/#mongodb-dbcommand-dbcmd.killSessions">
<code class="leafygreen-ui-1l06pbn">
killSessions
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Kill specified sessions.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/refreshSessions/#mongodb-dbcommand-dbcmd.refreshSessions">
<code class="leafygreen-ui-1l06pbn">
refreshSessions
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Refresh idle sessions.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/startSession/#mongodb-dbcommand-dbcmd.startSession">
<code class="leafygreen-ui-1l06pbn">
startSession
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Starts a new session.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-admin-commands">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Administration Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#administration-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="administration-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/cloneCollectionAsCapped/#mongodb-dbcommand-dbcmd.cloneCollectionAsCapped">
<code class="leafygreen-ui-1l06pbn">
cloneCollectionAsCapped
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Copies a non-capped collection as a new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-capped-collection">
capped collection
<!-- -->
.
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collMod/#mongodb-dbcommand-dbcmd.collMod">
<code class="leafygreen-ui-1l06pbn">
collMod
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Add options to a collection or modify a view definition.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/compact/#mongodb-dbcommand-dbcmd.compact">
<code class="leafygreen-ui-1l06pbn">
compact
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Defragments a collection and rebuilds the indexes.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/compactStructuredEncryptionData/#mongodb-dbcommand-dbcmd.compactStructuredEncryptionData">
<code class="leafygreen-ui-1l06pbn">
compactStructuredEncryptionData
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Compacts documents specified in the state collections and
deletes redundant documents.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/convertToCapped/#mongodb-dbcommand-dbcmd.convertToCapped">
<code class="leafygreen-ui-1l06pbn">
convertToCapped
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a non-capped collection to a capped collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/create/#mongodb-dbcommand-dbcmd.create">
<code class="leafygreen-ui-1l06pbn">
create
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Creates a collection or a view.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Builds one or more indexes for a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
<code class="leafygreen-ui-1l06pbn">
currentOp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a document that contains information on in-progress operations for the database instance.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/drop/#mongodb-dbcommand-dbcmd.drop">
<code class="leafygreen-ui-1l06pbn">
drop
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes the specified collection from the database.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropDatabase/#mongodb-dbcommand-dbcmd.dropDatabase">
<code class="leafygreen-ui-1l06pbn">
dropDatabase
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes the current database.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropConnections/#mongodb-dbcommand-dbcmd.dropConnections">
<code class="leafygreen-ui-1l06pbn">
dropConnections
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Drops outgoing connections to the specified list of hosts.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropIndexes/#mongodb-dbcommand-dbcmd.dropIndexes">
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes indexes from a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/filemd5/#mongodb-dbcommand-dbcmd.filemd5">
<code class="leafygreen-ui-1l06pbn">
filemd5
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-md5">
md5
</a>
hash for files stored using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-GridFS">
GridFS
<!-- -->
.
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/fsync/#mongodb-dbcommand-dbcmd.fsync">
<code class="leafygreen-ui-1l06pbn">
fsync
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Flushes pending writes to the storage layer and locks the database to allow backups.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/fsyncUnlock/#mongodb-dbcommand-dbcmd.fsyncUnlock">
<code class="leafygreen-ui-1l06pbn">
fsyncUnlock
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Unlocks one fsync lock.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getDefaultRWConcern/#mongodb-dbcommand-dbcmd.getDefaultRWConcern">
<code class="leafygreen-ui-1l06pbn">
getDefaultRWConcern
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Retrieves the global default read and write concern options
for the deployment.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getClusterParameter/#mongodb-dbcommand-dbcmd.getClusterParameter">
<code class="leafygreen-ui-1l06pbn">
getClusterParameter
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Retrieves cluster parameter values from all nodes in a cluster.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getParameter/#mongodb-dbcommand-dbcmd.getParameter">
<code class="leafygreen-ui-1l06pbn">
getParameter
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Retrieves configuration options.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Limited support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#limited-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killCursors/#mongodb-dbcommand-dbcmd.killCursors">
<code class="leafygreen-ui-1l06pbn">
killCursors
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Kills the specified cursors for a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killOp/#mongodb-dbcommand-dbcmd.killOp">
<code class="leafygreen-ui-1l06pbn">
killOp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Terminates an operation as specified by the operation ID.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Limited support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/listCollections/#mongodb-dbcommand-dbcmd.listCollections">
<code class="leafygreen-ui-1l06pbn">
listCollections
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a list of collections in the current database.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/listDatabases/#mongodb-dbcommand-dbcmd.listDatabases">
<code class="leafygreen-ui-1l06pbn">
listDatabases
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a document that lists all databases and returns basic database statistics.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/listIndexes/#mongodb-dbcommand-dbcmd.listIndexes">
<code class="leafygreen-ui-1l06pbn">
listIndexes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Lists all indexes for a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/logRotate/#mongodb-dbcommand-dbcmd.logRotate">
<code class="leafygreen-ui-1l06pbn">
logRotate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Rotates the MongoDB logs to prevent a single file from taking too much space.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/reIndex/#mongodb-dbcommand-dbcmd.reIndex">
<code class="leafygreen-ui-1l06pbn">
reIndex
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Rebuilds all indexes on a collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/renameCollection/#mongodb-dbcommand-dbcmd.renameCollection">
<code class="leafygreen-ui-1l06pbn">
renameCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Changes the name of an existing collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/rotateCertificates/#mongodb-dbcommand-dbcmd.rotateCertificates">
<code class="leafygreen-ui-1l06pbn">
rotateCertificates
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Performs online TLS certificate rotation.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Enables or disables features that persist data that are backwards-incompatible.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setIndexCommitQuorum/#mongodb-dbcommand-dbcmd.setIndexCommitQuorum">
<code class="leafygreen-ui-1l06pbn">
setIndexCommitQuorum
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Changes the minimum number of data-bearing members (i.e commit
quorum), including the primary, that must vote to commit an
in-progress
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-creation/#std-label-index-operations-replicated-build">
index build
</a>
before the primary marks
those indexes as ready.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setClusterParameter/#mongodb-dbcommand-dbcmd.setClusterParameter">
<code class="leafygreen-ui-1l06pbn">
setClusterParameter
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Modifies configuration options for all nodes in a replica set or
sharded cluster.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setParameter/#mongodb-dbcommand-dbcmd.setParameter">
<code class="leafygreen-ui-1l06pbn">
setParameter
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Modifies configuration options.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setDefaultRWConcern/#mongodb-dbcommand-dbcmd.setDefaultRWConcern">
<code class="leafygreen-ui-1l06pbn">
setDefaultRWConcern
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Sets the global default read and write concern options for the
deployment.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setUserWriteBlockMode/#mongodb-dbcommand-dbcmd.setUserWriteBlockMode">
<code class="leafygreen-ui-1l06pbn">
setUserWriteBlockMode
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Blocks and unblocks writes to the entire cluster.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shutdown/#mongodb-dbcommand-dbcmd.shutdown">
<code class="leafygreen-ui-1l06pbn">
shutdown
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shuts down the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
process.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, M5
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-1" target="_self">
<span>
M10 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/validateDBMetadata/#mongodb-dbcommand-dbcmd.validateDBMetadata">
<code class="leafygreen-ui-1l06pbn">
validateDBMetadata
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Checks that the stored metadata of a database or a collection
is valid within a particular API version.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Diagnostic Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#diagnostic-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="diagnostic-commands">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/buildInfo/#mongodb-dbcommand-dbcmd.buildInfo">
<code class="leafygreen-ui-1l06pbn">
buildInfo
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Displays statistics about the MongoDB build.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-dbcommand-dbcmd.collStats">
<code class="leafygreen-ui-1l06pbn">
collStats
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reports storage utilization statics for a specified collection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/connPoolStats/#mongodb-dbcommand-dbcmd.connPoolStats">
<code class="leafygreen-ui-1l06pbn">
connPoolStats
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reports statistics on the outgoing connections from this MongoDB instance to other MongoDB instances in the deployment.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/connectionStatus/#mongodb-dbcommand-dbcmd.connectionStatus">
<code class="leafygreen-ui-1l06pbn">
connectionStatus
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reports the authentication state for the current connection.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dataSize/#mongodb-dbcommand-dbcmd.dataSize">
<code class="leafygreen-ui-1l06pbn">
dataSize
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the data size for a range of data. For internal use.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dbHash/#mongodb-dbcommand-dbcmd.dbHash">
<code class="leafygreen-ui-1l06pbn">
dbHash
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns hash value a database and its collections.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dbStats/#mongodb-dbcommand-dbcmd.dbStats">
<code class="leafygreen-ui-1l06pbn">
dbStats
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reports storage utilization statistics for the specified database.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Limited support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#limited-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/explain/#mongodb-dbcommand-dbcmd.explain">
<code class="leafygreen-ui-1l06pbn">
explain
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information on the execution of various operations.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getCmdLineOpts/#mongodb-dbcommand-dbcmd.getCmdLineOpts">
<code class="leafygreen-ui-1l06pbn">
getCmdLineOpts
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a document with the run-time arguments to the MongoDB instance and their parsed options.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getLog/#mongodb-dbcommand-dbcmd.getLog">
<code class="leafygreen-ui-1l06pbn">
getLog
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns recent log messages.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/hostInfo/#mongodb-dbcommand-dbcmd.hostInfo">
<code class="leafygreen-ui-1l06pbn">
hostInfo
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns data that reflects the underlying host system.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/listCommands/#mongodb-dbcommand-dbcmd.listCommands">
<code class="leafygreen-ui-1l06pbn">
listCommands
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Lists all database commands provided by the current
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/lockInfo/#mongodb-dbcommand-dbcmd.lockInfo">
<code class="leafygreen-ui-1l06pbn">
lockInfo
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Internal command that returns information on locks that are
currently being held or pending. Only available for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/ping/#mongodb-dbcommand-dbcmd.ping">
<code class="leafygreen-ui-1l06pbn">
ping
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Internal command that tests intra-deployment connectivity.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/profile/#mongodb-dbcommand-dbcmd.profile">
<code class="leafygreen-ui-1l06pbn">
profile
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Interface for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
database profiler
<!-- -->
.
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-dbcommand-dbcmd.serverStatus">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a collection metrics on instance-wide resource utilization and status.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardConnPoolStats/#mongodb-dbcommand-dbcmd.shardConnPoolStats">
<code class="leafygreen-ui-1l06pbn">
shardConnPoolStats
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<em>
Removed in MongoDB 5.0.
</em>
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/connPoolStats/#mongodb-dbcommand-dbcmd.connPoolStats">
<code class="leafygreen-ui-1l06pbn">
connPoolStats
</code>
</a>
instead.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/top/#mongodb-dbcommand-dbcmd.top">
<code class="leafygreen-ui-1l06pbn">
top
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns raw usage statistics for each database in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/validate/#mongodb-dbcommand-dbcmd.validate">
<code class="leafygreen-ui-1l06pbn">
validate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Internal command that scans for a collection's data and indexes for correctness.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No support for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands" target="_self">
<span>
M0, M2, and M5 clusters
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#unsupported-commands-in-serverless-instances" target="_self">
<span>
serverless instances
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/whatsmyuri/#mongodb-dbcommand-dbcmd.whatsmyuri">
<code class="leafygreen-ui-1l06pbn">
whatsmyuri
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Internal command that returns information on the current client.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Auditing Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#auditing-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="auditing-commands">
</div>
</a>
</h2>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:50%"/>
<col style="width:20%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Atlas Support
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getAuditConfig/#mongodb-dbcommand-dbcmd.getAuditConfig">
<code class="leafygreen-ui-1l06pbn">
getAuditConfig
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Retrieves audit configurations from
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances.
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
Deprecated in MongoDB 7.1
</em>
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/logApplicationMessage/#mongodb-dbcommand-dbcmd.logApplicationMessage">
<code class="leafygreen-ui-1l06pbn">
logApplicationMessage
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Posts a custom message to the audit log.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setAuditConfig/#mongodb-dbcommand-dbcmd.setAuditConfig">
<code class="leafygreen-ui-1l06pbn">
setAuditConfig
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Sets audit configurations for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances at runtime.
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
Deprecated in MongoDB 7.1
</em>
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Atlas Search Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#atlas-search-commands" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="atlas-search-commands">
</div>
</a>
</h2>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
:
<!-- -->
<!-- -->
(
<em>
Also available starting in 6.0.7
</em>
)
</p>
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:70%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createSearchIndexes/#mongodb-dbcommand-dbcmd.createSearchIndexes">
<code class="leafygreen-ui-1l06pbn">
createSearchIndexes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Creates one or more
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/atlas-search-overview/#fts-indexes" target="_self">
<span>
Atlas Search indexes
</span>
</a>
on a specified collection.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropSearchIndex/#mongodb-dbcommand-dbcmd.dropSearchIndex">
<code class="leafygreen-ui-1l06pbn">
dropSearchIndex
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Deletes an existing
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/atlas-search-overview/#fts-indexes" target="_self">
<span>
Atlas Search index
</span>
</a>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/updateSearchIndex/#mongodb-dbcommand-dbcmd.updateSearchIndex">
<code class="leafygreen-ui-1l06pbn">
updateSearchIndex
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Updates an existing
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/atlas-search-overview/#fts-indexes" target="_self">
<span>
Atlas Search index
</span>
</a>
.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2dsphere/query/intersections-of-geojson-objects/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Query for Locations that Intersect a GeoJSON Object
<a class="headerlink leafygreen-ui-1kepx47" href="#query-for-locations-that-intersect-a-geojson-object" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="query-for-locations-that-intersect-a-geojson-object">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#about-this-task" target="_self">
<span>
About this Task
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#before-you-begin" target="_self">
<span>
Before You Begin
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
You can query for location data that intersects with a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geospatial-indexes-store-geojson">
GeoJSON
object
</a>
. For example, consider an
application that stores coordinates of gas stations. You can create a
GeoJSON
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-linestring">
LineString
</a>
that represents a road
trip, and query for gas stations that intersect with the road trip
route.
</p>
<p class="leafygreen-ui-1kp3ins">
To query for location data that intersects with a GeoJSON object, use
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/geoIntersects/#mongodb-query-op.-geoIntersects">
<code class="leafygreen-ui-1l06pbn">
$geoIntersects
</code>
</a>
operator:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.<collection>.find( {\n <location> : {\n $geoIntersects : {\n $geometry : {\n type : \"<geojson>\",\n coordinates : [ <coordinates> ]\n }\n }\n }\n } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<!-- --><<!-- -->collection<!-- -->><!-- -->.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><location field> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geoIntersects : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $geometry : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type : "<GeoJSON object type>",</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> coordinates : [ <coordinates> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
About this Task
<a class="headerlink leafygreen-ui-1kepx47" href="#about-this-task" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="about-this-task">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When you specify longitude and latitude coordinates, list the
<strong>
longitude
</strong>
first, and then
<strong>
latitude
</strong>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Valid longitude values are between
<code class="leafygreen-ui-1l06pbn">
-180
</code>
and
<code class="leafygreen-ui-1l06pbn">
180
</code>
, both
inclusive.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Valid latitude values are between
<code class="leafygreen-ui-1l06pbn">
-90
</code>
and
<code class="leafygreen-ui-1l06pbn">
90
</code>
, both
inclusive.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A location intersects with an object if it shares at least one point
with the specified object. This includes objects that have a shared
edge.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$geoIntersects
</code>
does not require a geospatial index. However, a
geospatial index improves query performance. Only the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/#std-label-2dsphere-index">
2dsphere
</a>
geospatial index supports
<code class="leafygreen-ui-1l06pbn">
$geoIntersects
</code>
. For
more information see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/create/#std-label-create-2dsphere-index">
Create a 2dsphere Index
<!-- -->
.
</a>
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Before You Begin
<a class="headerlink leafygreen-ui-1kepx47" href="#before-you-begin" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="before-you-begin">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
gasStations
</code>
collection that contains these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.gasStations.insertMany( [\n {\n loc: { type: \"Point\", coordinates: [ -106.31, 35.65 ] },\n state: \"New Mexico\",\n country: \"United States\",\n name: \"Horizons Gas Station\"\n },\n {\n loc: { type: \"Point\", coordinates: [ -122.62, 40.75 ] },\n state: \"California\",\n country: \"United States\",\n name: \"Car and Truck Rest Area\"\n },\n {\n loc: { type: \"Point\", coordinates: [ -72.71, 44.15 ] },\n state: \"Vermont\",\n country: \"United States\",\n name: \"Ready Gas and Snacks\"\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">gasStations</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">106.31</span>, <span class="lg-highlight-number">35.65</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"New Mexico"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">country</span>: <span class="lg-highlight-string">"United States"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Horizons Gas Station"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">122.62</span>, <span class="lg-highlight-number">40.75</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"California"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">country</span>: <span class="lg-highlight-string">"United States"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Car and Truck Rest Area"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Point"</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">72.71</span>, <span class="lg-highlight-number">44.15</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"Vermont"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">country</span>: <span class="lg-highlight-string">"United States"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Ready Gas and Snacks"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
$geoIntersects
</code>
query specifies a
<code class="leafygreen-ui-1l06pbn">
LineString
</code>
containing four points and returns documents that intersect with the
line:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.gasStations.find( {\n loc: {\n $geoIntersects: {\n $geometry: {\n type: \"LineString\",\n coordinates: [\n [ -105.82, 33.87 ],\n [ -106.01, 34.09 ],\n [ -106.31, 35.65 ],\n [ -107.39, 35.98 ]\n ]\n }\n }\n }\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">gasStations</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geoIntersects</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geometry</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"LineString"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">coordinates</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <!-- -->-<span class="lg-highlight-number">105.82</span>, <span class="lg-highlight-number">33.87</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <!-- -->-<span class="lg-highlight-number">106.01</span>, <span class="lg-highlight-number">34.09</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <!-- -->-<span class="lg-highlight-number">106.31</span>, <span class="lg-highlight-number">35.65</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <!-- -->-<span class="lg-highlight-number">107.39</span>, <span class="lg-highlight-number">35.98</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId(\"63f658d45e5eefbdfef81ca4\"),\n loc: { type: 'Point', coordinates: [ -106.31, 35.65 ] },\n state: 'New Mexico',\n country: 'United States',\n name: 'Horizons Gas Station'\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"63f658d45e5eefbdfef81ca4"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loc</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'Point'</span>, <span class="lg-highlight-attr">coordinates</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">106.31</span>, <span class="lg-highlight-number">35.65</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">'New Mexico'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">country</span>: <span class="lg-highlight-string">'United States'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Horizons Gas Station'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/geoIntersects/#mongodb-query-op.-geoIntersects">
<code class="leafygreen-ui-1l06pbn">
$geoIntersects
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-linestring">
<code class="leafygreen-ui-1l06pbn">
LineString
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/query/geojson-bound-by-polygon/#std-label-2dsphere-query-geojson-objects-polygon">
Query for Locations Bound by a Polygon
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/restrictions/#std-label-geospatial-restrictions">
Geospatial Index Restrictions
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/configure-linux-iptables-firewall/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Configure Linux
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
Firewall for Self-Managed Deployments
<a class="headerlink leafygreen-ui-1kepx47" href="#configure-linux-iptables-firewall-for-self-managed-deployments" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="configure-linux-iptables-firewall-for-self-managed-deployments">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#overview" target="_self">
<span>
Overview
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#patterns" target="_self">
<span>
Patterns
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#change-default-policy-to-drop" target="_self">
<span>
Change Default Policy to
<code class="css-1kekw28 e1wawog0">
DROP
</code>
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#manage-and-maintain-iptables-configuration" target="_self">
<span>
Manage and Maintain
<code class="css-1kekw28 e1wawog0">
iptables
</code>
Configuration
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
On contemporary Linux systems, the
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
program provides
methods for managing the Linux Kernel's
<code class="leafygreen-ui-1l06pbn">
netfilter
</code>
or network
packet filtering capabilities. These firewall rules make it possible
for administrators to control what hosts can connect to the system,
and limit risk exposure by limiting the hosts that can connect to a
system.
</p>
<p class="leafygreen-ui-1kp3ins">
This document outlines basic firewall configurations for
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
firewalls on Linux. Use these approaches as a starting point for your
larger networking organization. For a detailed overview of security
practices and risk management for MongoDB, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/security/">
Security
<!-- -->
.
</a>
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Overview
<a class="headerlink leafygreen-ui-1kepx47" href="#overview" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="overview">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Rules in
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
configurations fall into chains, which describe
the process for filtering and processing specific streams of
traffic. Chains have an order, and packets must pass through earlier
rules in a chain to reach later rules. This document addresses only the
following two chains:
</p>
<dl>
<dt>
<code class="leafygreen-ui-1l06pbn">
INPUT
</code>
</dt>
<dd class="css-1ufxuu7">
Controls all incoming traffic.
</dd>
<dt>
<code class="leafygreen-ui-1l06pbn">
OUTPUT
</code>
</dt>
<dd class="css-1ufxuu7">
Controls all outgoing traffic.
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
Given the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/default-mongodb-port/#std-label-default-mongodb-port">
default ports
</a>
of all
MongoDB processes, you must configure networking rules that permit
<em>
only
</em>
required communication between your application and the appropriate
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances.
</p>
<p class="leafygreen-ui-1kp3ins">
Be aware that, by default, the default policy of
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
is to
allow all connections and traffic unless explicitly disabled. The
configuration changes outlined in this document will create rules that
explicitly allow traffic from specific addresses and on specific
ports, using a default policy that drops all traffic that is not
explicitly allowed. When you have properly configured your
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
rules to allow only the traffic that you want to permit,
you can
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-iptables-change-default-policy-to-drop" target="_self">
<span>
Change Default Policy to
<code class="leafygreen-ui-1l06pbn">
DROP
</code>
.
</span>
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Patterns
<a class="headerlink leafygreen-ui-1kepx47" href="#patterns" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="patterns">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This section contains a number of patterns and examples for
configuring
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
for use with MongoDB deployments. If you have
configured different ports using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.port">
<code class="leafygreen-ui-1l06pbn">
port
</code>
</a>
configuration
setting, you will need to modify the rules accordingly.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-iptables-basic-rule-set">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Traffic to and from
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Instances
<a class="headerlink leafygreen-ui-1kepx47" href="#traffic-to-and-from-mongod-instances" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="traffic-to-and-from-mongod-instances">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This pattern is applicable to all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances running
as standalone instances or as part of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The goal of this pattern is to explicitly allow traffic to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance from the application server. In the
following examples, replace
<code class="leafygreen-ui-1l06pbn">
<ip-address>
</code>
with the IP address of
the application server:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables -A INPUT -s <ip-address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT\niptables -A OUTPUT -d <ip-address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables -A INPUT -s <ip-address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT</td></tr><tr class=""><td class="leafygreen-ui-7razhx">iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The first rule allows all incoming traffic from
<code class="leafygreen-ui-1l06pbn">
<ip-address>
</code>
on
port
<code class="leafygreen-ui-1l06pbn">
27017
</code>
, which allows the application server to connect to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance. The second rule, allows outgoing traffic
from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
to reach the application server.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Optional
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If you have only one application server, you can replace
<code class="leafygreen-ui-1l06pbn">
<ip-address>
</code>
with either the IP address itself, such as:
<code class="leafygreen-ui-1l06pbn">
198.51.100.55
</code>
. You can also express this using CIDR notation as
<code class="leafygreen-ui-1l06pbn">
198.51.100.55/32
</code>
. If you want to permit a larger block of
possible IP addresses you can allow traffic from a
<code class="leafygreen-ui-1l06pbn">
/24
</code>
using
one of the following specifications for the
<code class="leafygreen-ui-1l06pbn">
<ip-address>
</code>
, as
follows:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"10.10.10.10/24\n10.10.10.10/255.255.255.0","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">10.10.10.10/24</td></tr><tr class=""><td class="leafygreen-ui-7razhx">10.10.10.10/255.255.255.0</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Traffic to and from
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
Instances
<a class="headerlink leafygreen-ui-1kepx47" href="#traffic-to-and-from-mongos-instances" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="traffic-to-and-from-mongos-instances">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances provide query routing for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded
clusters
</a>
. Clients connect to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances, which
behave from the client's perspective as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances. In turn, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
connects to all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances that are components of the sharded
cluster.
</p>
<p class="leafygreen-ui-1kp3ins">
Use the same
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
command to allow traffic to and from these
instances as you would from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances that are
members of the replica set. Take the configuration outlined in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-iptables-basic-rule-set" target="_self">
<span>
Traffic to and from
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Instances
</span>
</a>
section as an example.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Traffic to and from a MongoDB Config Server
<a class="headerlink leafygreen-ui-1kepx47" href="#traffic-to-and-from-a-mongodb-config-server" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="traffic-to-and-from-a-mongodb-config-server">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Config servers host the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-database">
config database
</a>
that stores metadata
for sharded clusters. Config servers listen for connections on port
<code class="leafygreen-ui-1l06pbn">
27019
</code>
. As a result, add the following
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
rules to the
config server to allow incoming and outgoing connection on port
<code class="leafygreen-ui-1l06pbn">
27019
</code>
, for connection to the other config servers.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables -A INPUT -s <ip-address> -p tcp --destination-port 27019 -m state --state NEW,ESTABLISHED -j ACCEPT\niptables -A OUTPUT -d <ip-address> -p tcp --source-port 27019 -m state --state ESTABLISHED -j ACCEPT","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables -A INPUT -s <ip-address> -p tcp --destination-port 27019 -m state --state NEW,ESTABLISHED -j ACCEPT</td></tr><tr class=""><td class="leafygreen-ui-7razhx">iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27019 -m state --state ESTABLISHED -j ACCEPT</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Replace
<code class="leafygreen-ui-1l06pbn">
<ip-address>
</code>
with the address or address space of
<em>
all
</em>
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
that provide config servers.
</p>
<p class="leafygreen-ui-1kp3ins">
Additionally, config servers need to allow incoming connections from
all of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances in the cluster
<em>
and
</em>
all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances in the cluster. Add rules that
resemble the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables -A INPUT -s <ip-address> -p tcp --destination-port 27019 -m state --state NEW,ESTABLISHED -j ACCEPT","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables -A INPUT -s <ip-address> -p tcp --destination-port 27019 -m state --state NEW,ESTABLISHED -j ACCEPT</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Replace
<code class="leafygreen-ui-1l06pbn">
<ip-address>
</code>
with the address of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances and the shard
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Traffic to and from a MongoDB Shard Server
<a class="headerlink leafygreen-ui-1kepx47" href="#traffic-to-and-from-a-mongodb-shard-server" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="traffic-to-and-from-a-mongodb-shard-server">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharding-background">
Shard servers
</a>
default to port number
<code class="leafygreen-ui-1l06pbn">
27018
</code>
. You must configure the following
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
rules to allow
traffic to and from each shard:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables -A INPUT -s <ip-address> -p tcp --destination-port 27018 -m state --state NEW,ESTABLISHED -j ACCEPT\niptables -A OUTPUT -d <ip-address> -p tcp --source-port 27018 -m state --state ESTABLISHED -j ACCEPT","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables -A INPUT -s <ip-address> -p tcp --destination-port 27018 -m state --state NEW,ESTABLISHED -j ACCEPT</td></tr><tr class=""><td class="leafygreen-ui-7razhx">iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27018 -m state --state ESTABLISHED -j ACCEPT</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Replace the
<code class="leafygreen-ui-1l06pbn">
<ip-address>
</code>
specification with the IP address of all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
. This allows you to permit incoming and outgoing
traffic between all shards including constituent replica set members,
to:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances in the shard's replica sets.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances in other shards.
<a class="footnote-reference header-buffer" href="#footnote-migrations" id="ref-migrations-id1">
[
<!-- -->
1
<!-- -->
]
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Furthermore, shards need to be able make outgoing connections to:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances in the config servers.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Create a rule that resembles the following, and replace the
<code class="leafygreen-ui-1l06pbn">
<ip-address>
</code>
with the address of the config servers and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27018 -m state --state ESTABLISHED -j ACCEPT","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27018 -m state --state ESTABLISHED -j ACCEPT</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-migrations" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-migrations-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
All shards in a cluster need to be able to
communicate with all other shards to facilitate
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunk
</a>
and
balancing operations.
</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Provide Access For Monitoring Systems
<a class="headerlink leafygreen-ui-1kepx47" href="#provide-access-for-monitoring-systems" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="provide-access-for-monitoring-systems">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongostat/#mongodb-binary-bin.mongostat" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongostat
</code>
</span>
</a>
diagnostic tool, when running with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongostat/#std-option-mongostat.--discover" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--discover
</code>
</span>
</a>
needs to be able to reach
all components of a cluster, including the config servers, the shard
servers, and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-iptables-change-default-policy-to-drop">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Change Default Policy to
<code class="leafygreen-ui-1l06pbn">
DROP
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#change-default-policy-to-drop" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="change-default-policy-to-drop">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The default policy for
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
chains is to allow all
traffic. After completing all
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
configuration changes, you
<em>
must
</em>
change the default policy to
<code class="leafygreen-ui-1l06pbn">
DROP
</code>
so that all traffic that
isn't explicitly allowed as above will not be able to reach components
of the MongoDB deployment. Issue the following commands to change this
policy:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables -P INPUT DROP\n\niptables -P OUTPUT DROP","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables -P INPUT DROP</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">iptables -P OUTPUT DROP</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Manage and Maintain
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
Configuration
<a class="headerlink leafygreen-ui-1kepx47" href="#manage-and-maintain-iptables-configuration" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="manage-and-maintain-iptables-configuration">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This section contains a number of basic operations for managing and
using
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
. There are various front end tools that automate
some aspects of
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
configuration, but at the core all
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
front ends provide the same basic functionality:
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-iptables-make-all-rules-persistent">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Make all
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
Rules Persistent
<a class="headerlink leafygreen-ui-1kepx47" href="#make-all-iptables-rules-persistent" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="make-all-iptables-rules-persistent">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
By default all
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
rules are only stored in memory. When
your system restarts, your firewall rules will revert to their
defaults. When you have tested a rule set and have guaranteed that it
effectively controls traffic you can use the following operations to
you should make the rule set persistent.
</p>
<p class="leafygreen-ui-1kp3ins">
On Red Hat Enterprise Linux, Fedora Linux, and related distributions
you can issue the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"service iptables save","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">service iptables save</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
On Debian, Ubuntu, and related distributions, you can use the
following command to dump the
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
rules to the
<code class="leafygreen-ui-1l06pbn">
/etc/iptables.conf
</code>
file:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables-save > /etc/iptables.conf","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables-save > <!-- -->/etc/iptables.conf</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Run the following operation to restore the network rules:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables-restore < /etc/iptables.conf","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables-restore < <!-- -->/etc/iptables.conf</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Place this command in your
<code class="leafygreen-ui-1l06pbn">
rc.local
</code>
file, or in the
<code class="leafygreen-ui-1l06pbn">
/etc/network/if-up.d/iptables
</code>
file with other similar operations.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
List all
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
Rules
<a class="headerlink leafygreen-ui-1kepx47" href="#list-all-iptables-rules" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="list-all-iptables-rules">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To list all of currently applied
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
rules, use the following
operation at the system shell.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables -L","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables -L</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Flush all
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
Rules
<a class="headerlink leafygreen-ui-1kepx47" href="#flush-all-iptables-rules" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="flush-all-iptables-rules">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If you make a configuration mistake when entering
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
rules
or simply need to revert to the default rule set, you can use the
following operation at the system shell to flush all rules:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"iptables -F","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">iptables -F</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you've already made your
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
rules persistent, you will
need to repeat the appropriate procedure in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-iptables-make-all-rules-persistent" target="_self">
<span>
Make all
<code class="leafygreen-ui-1l06pbn">
iptables
</code>
Rules Persistent
</span>
</a>
section.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sh.reshardCollection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.reshardCollection()
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.reshardcollection--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sh.reshardcollection--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#considerations" target="_self">
<span>
Considerations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#resharding-process" target="_self">
<span>
Resharding Process
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
sh.reshardCollection(namespace, key, unique, options)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.reshardCollection" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-sh.reshardCollection">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.reshardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.reshardCollection()
</code>
</span>
</a>
method changes the shard key for
a collection and changes the distribution of your data.
</p>
<p class="leafygreen-ui-1kp3ins">
Before you reshard a collection, read the the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-reshard-requirements">
reshard
requirements
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-resharding-limitations">
reshard limitations
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for database commands or language-specific drivers,
such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For the database command, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/reshardCollection/#mongodb-dbcommand-dbcmd.reshardCollection">
<code class="leafygreen-ui-1l06pbn">
reshardCollection
</code>
</a>
command
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sh.reshardCollection()
</code>
takes the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
namespace
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-namespace">
namespace
</a>
of the collection to shard in the form
<code class="leafygreen-ui-1l06pbn">
"<database>.<collection>"
</code>
.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
key
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The document that specifies the new field or fields to use as the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-key/#std-label-sharding-shard-key">
shard key
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ <field1>: <1|"hashed">, ... }
</code>
</p>
<p class="leafygreen-ui-1kp3ins">
Set the field values to either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
1
</code>
for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/ranged-sharding/#std-label-sharding-ranged">
range-based sharding
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"hashed"
</code>
to specify a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#std-label-hashed-shard-keys">
hashed shard key
<!-- -->
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-key/#std-label-sharding-shard-key-indexes">
Shard Key Indexes
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
unique
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. Specify whether there is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/">
uniqueness
</a>
constraint on the shard key. Only
<code class="leafygreen-ui-1l06pbn">
false
</code>
is supported. Defaults to
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
options
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. A document containing optional fields, including
<code class="leafygreen-ui-1l06pbn">
numInitialChunks
</code>
,
<code class="leafygreen-ui-1l06pbn">
collation
</code>
and
<code class="leafygreen-ui-1l06pbn">
zones
</code>
.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
options
</code>
field supports the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
numInitialChunks
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. Specifies the initial number of chunks to create
across all shards in the cluster when resharding a collection.
The default is the number of chunks that exist for the
collection under the current shard key pattern. MongoDB will
then create and balance chunks across the cluster. The
<code class="leafygreen-ui-1l06pbn">
numInitialChunks
</code>
must result in less than
<code class="leafygreen-ui-1l06pbn">
8192
</code>
per shard.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
collation
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. If the collection specified to
<code class="leafygreen-ui-1l06pbn">
reshardCollection
</code>
has a default
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
, you
<em>
must
</em>
include a
collation document with
<code class="leafygreen-ui-1l06pbn">
{ locale : "simple" }
</code>
, or the
<code class="leafygreen-ui-1l06pbn">
reshardCollection
</code>
command fails.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
zones
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. To maintain or add
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zone-sharding">
zones
</a>
,
specify the zones for your collection in an array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n min: <document>,\n max: <document>,\n zone: <string> | null\n },\n ...\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">min</span>: <!-- --><document with same shape as shardkey>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> max: <document with same shape as shardkey>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> zone: <string> | null</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ...</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This method is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is supported in all MongoDB Atlas clusters.
For information on Atlas support for all commands, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="considerations">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Index builds that occur during resharding might silently
fail.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Do not create indexes during the resharding process.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Do not start the resharding process if there are ongoing index builds.
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-resharding-process-details">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Resharding Process
<a class="headerlink leafygreen-ui-1kepx47" href="#resharding-process" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resharding-process">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
In a collection resharding operation, a shard can be a:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<strong>
donor
</strong>
, which currently stores
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunks
</a>
for the
sharded collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<strong>
recipient
</strong>
, which stores new chunks for the sharded collection
based on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
shard keys
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/#std-label-zone-sharding">
zones
<!-- -->
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
A shard can be donor and a recipient at the same time. The set of donor
shards is identical to the recipient shards, unless you use zones.
</p>
<p class="leafygreen-ui-1kp3ins">
The config server primary is always the resharding coordinator and
starts each phase of the resharding operation.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Initialization Phase
<a class="headerlink leafygreen-ui-1kepx47" href="#initialization-phase" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="initialization-phase">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
During the initialization phase, the resharding coordinator determines
the new data distribution for the sharded collection.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Index Phase
<a class="headerlink leafygreen-ui-1kepx47" href="#index-phase" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="index-phase">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
During the index phase:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Each shard recipient creates a new, empty sharded collection with the
same collection options as the existing sharded collection. This new
sharded collection is the target for where recipient shards write the
new data.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Each shard recipient builds the necessary new indexes. These include
all existing indexes on the sharded collection and an index compatible
with the new shard key pattern if such an index doesn't already exist on
the sharded collection.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Clone, Apply, and Catch-up Phase
<a class="headerlink leafygreen-ui-1kepx47" href="#clone--apply--and-catch-up-phase" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="clone--apply--and-catch-up-phase">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
During the clone, apply, and catch-up phase:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Each shard recipient clones an initial copy of the documents it would
own under the new shard key.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Each shard recipient begins applying oplog entries from operations
that happened after the recipient cloned the data.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When the estimate for the time remaining to complete the resharding
operation is under
<strong>
two seconds
</strong>
, the resharding coordinator blocks
writes for the collection.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If required, you can manually force the resharding operation to
complete by issuing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.commitReshardCollection/#mongodb-method-sh.commitReshardCollection">
<code class="leafygreen-ui-1l06pbn">
sh.commitReshardCollection()
</code>
</a>
method. This is useful if the current time estimate to complete the
resharding operation is an acceptable duration for your collection
to block writes. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.commitReshardCollection/#mongodb-method-sh.commitReshardCollection">
<code class="leafygreen-ui-1l06pbn">
sh.commitReshardCollection()
</code>
</a>
method
blocks writes early and forces the resharding operation to
complete. During the time period where writes are blocked your
application experiences an increase in latency.
</p>
</div>
</div>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-resharding-commit-phase-method">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Commit Phase
<a class="headerlink leafygreen-ui-1kepx47" href="#commit-phase" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="commit-phase">
</div>
</a>
</h3>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
After the resharding process reaches the commit phase, the process
cannot be ended with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.abortReshardCollection/#mongodb-method-sh.abortReshardCollection">
<code class="leafygreen-ui-1l06pbn">
sh.abortReshardCollection()
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When all shards have reached strict consistency, the resharding
coordinator commits the resharding operation and installs the new
routing table.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The resharding coordinator instructs each donor and recipient shard
primary, independently, to rename the temporary sharded collection.
The temporary collection becomes the new resharded collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Each donor shard drops the old sharded collection.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
Reshard a Collection
</a>
</p>
</div>
</div>
</li>
</ul>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Reshard a Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#reshard-a-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="reshard-a-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example reshards the
<code class="leafygreen-ui-1l06pbn">
sales.orders
</code>
collection with the
new shard key
<code class="leafygreen-ui-1l06pbn">
{ order_id: 1 }
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.reshardCollection( \"sales.orders\", { order_id: 1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">reshardCollection</span>( <span class="lg-highlight-string">"sales.orders"</span>, <!-- -->{ <span class="lg-highlight-attr">order_id</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n ok: 1,\n '$clusterTime': {\n clusterTime: Timestamp(1, 1624887954),\n signature: {\n hash: Binary(Buffer.from(\"0000000000000000000000000000000000000000\", \"hex\"), 0),\n keyId: 0\n }\n },\n operationTime: Timestamp(1, 1624887947)\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'$clusterTime'</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clusterTime</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">1624887954</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">signature</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hash</span>: <span class="lg-highlight-title lg-highlight-class">Binary</span>(<span class="lg-highlight-title lg-highlight-class">Buffer</span>.<span class="lg-highlight-title lg-highlight-function">from</span>(<span class="lg-highlight-string">"0000000000000000000000000000000000000000"</span>, <span class="lg-highlight-string">"hex"</span>)<!-- -->, <span class="lg-highlight-number">0</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyId</span>: <span class="lg-highlight-number">0</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">operationTime</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">1624887947</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Reshard a Collection with Zones
<a class="headerlink leafygreen-ui-1kepx47" href="#reshard-a-collection-with-zones" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="reshard-a-collection-with-zones">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Reshard a collection with zones when you need to adjust the distribution
of data across the shards in your cluster to meet changing requirements or
to improve performance.
</p>
<p class="leafygreen-ui-1kp3ins">
In the following example, the
<code class="leafygreen-ui-1l06pbn">
test.scores
</code>
collection resides on
<code class="leafygreen-ui-1l06pbn">
shard0
</code>
and
<code class="leafygreen-ui-1l06pbn">
shard1
</code>
. The current shard key is
<code class="leafygreen-ui-1l06pbn">
{ _id: 1}
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"In this example, this zone is called NewZone.","name":"Add shards to a new zone"},{"@type":"HowToStep","text":"The resharding operation adds the shards in zone NewZone as recipients.\nThe database primary shard is added as a recipient as a backstop for any\nmissing ranges in the zone definition. If there are no missing ranges, the\ncollection is cloned on shards in the \"NewZone\", such as shard2 and\nshard3 in this example. sh.reshardCollection returns the following:","name":"Run sh.reshardCollection with the new zone information"}],"name":"Reshard a Collection with Zones","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Add shards to a new zone
<a class="headerlink leafygreen-ui-1kepx47" href="#add-shards-to-a-new-zone" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="add-shards-to-a-new-zone">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
In this example, this zone is called
<code class="leafygreen-ui-1l06pbn">
NewZone
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.addShardToZone( \"shard2\", \"NewZone\" )\nsh.addShardToZone( \"shard3\", \"NewZone\" )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">addShardToZone</span>( <span class="lg-highlight-string">"shard2"</span>, <span class="lg-highlight-string">"NewZone"</span> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">addShardToZone</span>( <span class="lg-highlight-string">"shard3"</span>, <span class="lg-highlight-string">"NewZone"</span> )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Run
<code class="leafygreen-ui-1l06pbn">
sh.reshardCollection
</code>
with the new zone information
<a class="headerlink leafygreen-ui-1kepx47" href="#run-sh.reshardcollection-with-the-new-zone-information" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="run-sh.reshardcollection-with-the-new-zone-information">
</div>
</a>
</h4>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.reshardCollection(\n \"test.scores\",\n { \"studentId\": 1, \"testId\": 1},\n { zones: [ {\n min: { \"studentId\": MinKey(), \"testId\": MinKey() },\n max: { \"studentId\": MaxKey(), \"testId\": MaxKey() },\n zone: \"NewZone\" }\n ]\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">reshardCollection</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"test.scores"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"studentId"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"testId"</span>: <span class="lg-highlight-number">1</span>}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">zones</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">min</span>: <!-- -->{ <span class="lg-highlight-string">"studentId"</span>: <span class="lg-highlight-title lg-highlight-class">MinKey</span>(<!-- -->)<!-- -->, <span class="lg-highlight-string">"testId"</span>: <span class="lg-highlight-title lg-highlight-class">MinKey</span>(<!-- -->) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">max</span>: <!-- -->{ <span class="lg-highlight-string">"studentId"</span>: <span class="lg-highlight-title lg-highlight-class">MaxKey</span>(<!-- -->)<!-- -->, <span class="lg-highlight-string">"testId"</span>: <span class="lg-highlight-title lg-highlight-class">MaxKey</span>(<!-- -->) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">zone</span>: <span class="lg-highlight-string">"NewZone"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The resharding operation adds the shards in zone
<code class="leafygreen-ui-1l06pbn">
NewZone
</code>
as recipients.
The database primary shard is added as a recipient as a backstop for any
missing ranges in the zone definition. If there are no missing ranges, the
collection is cloned on shards in the "NewZone", such as
<code class="leafygreen-ui-1l06pbn">
shard2
</code>
and
<code class="leafygreen-ui-1l06pbn">
shard3
</code>
in this example.
<code class="leafygreen-ui-1l06pbn">
sh.reshardCollection
</code>
returns the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n ok: 1,\n '$clusterTime': {\n clusterTime: Timestamp( { t: 1699484530, i: 54 } ),\n signature: {\n hash: Binary.createFromBase64( \"90ApBDrSSi4XnCpV3OWIH4OGO0Y=\", 0 ),\n keyId: Long( \"7296989036055363606\" )\n } },\n operationTime: Timestamp( { t: 1699484530, i: 54 } )\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'$clusterTime'</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clusterTime</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>( <!-- -->{ <span class="lg-highlight-attr">t</span>: <span class="lg-highlight-number">1699484530</span>, <span class="lg-highlight-attr">i</span>: <span class="lg-highlight-number">54</span> } )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">signature</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hash</span>: <span class="lg-highlight-title lg-highlight-class">Binary</span>.<span class="lg-highlight-title lg-highlight-function">createFromBase64</span>( <span class="lg-highlight-string">"90ApBDrSSi4XnCpV3OWIH4OGO0Y="</span>, <span class="lg-highlight-number">0</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyId</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>( <span class="lg-highlight-string">"7296989036055363606"</span> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">operationTime</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>( <!-- -->{ <span class="lg-highlight-attr">t</span>: <span class="lg-highlight-number">1699484530</span>, <span class="lg-highlight-attr">i</span>: <span class="lg-highlight-number">54</span> } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
Reshard a Collection
</a>
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/clearJumboFlag/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
clearJumboFlag
<a class="headerlink leafygreen-ui-1kepx47" href="#clearjumboflag" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="clearjumboflag">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#access-control" target="_self">
<span>
Access Control
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
clearJumboFlag
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.clearJumboFlag" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.clearJumboFlag">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Clears the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-jumbo-chunks">
jumbo
</a>
flag for a chunk. To use the
command, issue the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.clearJumboFlag" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
clearJumboFlag
</code>
</span>
</a>
command on a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is not supported in serverless instances.
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Th command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand(\n {\n clearJumboFlag: \"<database>.<collection>\",\n bounds: <array>\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clearJumboFlag</span>: <span class="lg-highlight-string">"<database>.<collection>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bounds</span>: <<!-- -->array<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<strong>
-OR-
</strong>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// Cannot use for collections with hashed shard keys\n\ndb.adminCommand(\n {\n clearJumboFlag: \"<database>.<collection>\",\n find: <query>\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Cannot use for </span><span class="lg-highlight-comment">collections</span><span class="lg-highlight-comment"> with hashed shard </span><span class="lg-highlight-comment">keys</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clearJumboFlag</span>: <span class="lg-highlight-string">"<database>.<collection>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">find</span>: <<!-- -->query<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.clearJumboFlag" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
clearJumboFlag
</code>
</span>
</a>
command takes the following fields as
arguments:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-namespace" target="_self">
<span>
clearJumboFlag
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-clearJumboFlag-namespace">
</span>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-namespace">
namespace
</a>
of the sharded
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-collection">
collection
</a>
with the
jumbo chunk(s).
</p>
<p class="leafygreen-ui-1kp3ins">
Specify the collection's full namespace, including the
database name (i.e. "<database>.<collection>")
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-bounds" target="_self">
<span>
bounds
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-clearJumboFlag-bounds">
</span>
<p class="leafygreen-ui-1kp3ins">
The exact bounds of a specific chunk. The array must consist of two
documents that specify the lower and upper shard key values of a chunk
to move:
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
[ { <shardKey> : <minValue> },{ <shardKey> : <maxValue> } ]
</code>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Specify either the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-bounds" target="_self">
<span>
bounds
</span>
</a>
field or the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-find" target="_self">
<span>
find
</span>
</a>
field but
not both.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the collection uses a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-hashed-shard-key">
hashed shard key
</a>
, use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-bounds" target="_self">
<span>
bounds
</span>
</a>
field.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-find" target="_self">
<span>
find
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-clearJumboFlag-find">
</span>
<p class="leafygreen-ui-1kp3ins">
A specific shard key and its value contained in the jumbo chunk.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ <shardKey> : <value> }
</code>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Specify either the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-bounds" target="_self">
<span>
bounds
</span>
</a>
field or the
<code class="leafygreen-ui-1l06pbn">
find
</code>
field but not both.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the collection uses a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-hashed-shard-key">
hashed shard key
</a>
, do
<strong>
not
</strong>
use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-find" target="_self">
<span>
find
</span>
</a>
field.
Use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-bounds" target="_self">
<span>
bounds
</span>
</a>
instead.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Access Control
<a class="headerlink leafygreen-ui-1kepx47" href="#access-control" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="access-control">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
On systems running with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.authorization">
<code class="leafygreen-ui-1l06pbn">
authorization
</code>
</a>
, the user
must have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-clearJumboFlag">
<code class="leafygreen-ui-1l06pbn">
clearJumboFlag
</code>
</a>
privilege actions on the
<code class="leafygreen-ui-1l06pbn">
{
db: "", collection: "" }
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-all-but-system-collections">
resource
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The built-in role
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-clusterManager">
<code class="leafygreen-ui-1l06pbn">
clusterManager
</code>
</a>
provides the appropriate
privileges.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Clear Jumbo Flag for a Chunk (Range-Based Shard Key)
<a class="headerlink leafygreen-ui-1kepx47" href="#clear-jumbo-flag-for-a-chunk--range-based-shard-key-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="clear-jumbo-flag-for-a-chunk--range-based-shard-key-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-method-sh.status">
<code class="leafygreen-ui-1l06pbn">
sh.status()
</code>
</a>
includes the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-data-sh.status.databases.-collection-.chunk-details">
<code class="leafygreen-ui-1l06pbn">
sh.status.databases.<collection>.chunk-details
</code>
</a>
for the
<code class="leafygreen-ui-1l06pbn">
test.jumbo
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"... // Content omitted for brevity\n\ntest.jumbo\n shard key: { \"x\" : 1 }\n unique: false\n balancing: true\n chunks:\n shardA 2\n shardB 2\n { \"x\" : { \"$minKey\" : 1 } } -->> { \"x\" : 1 } on : shardB Timestamp(3, 0)\n { \"x\" : 1 } -->> { \"x\" : 2 } on : shardA Timestamp(6, 1) jumbo\n { \"x\" : 2 } -->> { \"x\" : 3 } on : shardA Timestamp(5, 1) jumbo\n { \"x\" : 3 } -->> { \"x\" : { \"$maxKey\" : 1 } } on : shardB Timestamp(6, 0)","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">...<!-- --> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Content omitted for </span><span class="lg-highlight-comment">brevity</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">test.<span class="lg-highlight-property">jumbo</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shard <span class="lg-highlight-attr">key</span>: <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unique</span>: <span class="lg-highlight-literal">false</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">balancing</span>: <span class="lg-highlight-literal">true</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">chunks</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->shardA <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shardB <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"$minKey"</span> <!-- -->: <span class="lg-highlight-number">1</span> } } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">1</span> } on <!-- -->: <!-- -->shardB <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">3</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">1</span> } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">2</span> } on <!-- -->: <!-- -->shardA <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">6</span>, <span class="lg-highlight-number">1</span>) jumbo</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">2</span> } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">3</span> } on <!-- -->: <!-- -->shardA <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">5</span>, <span class="lg-highlight-number">1</span>) jumbo</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">3</span> } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"$maxKey"</span> <!-- -->: <span class="lg-highlight-number">1</span> } } on <!-- -->: <!-- -->shardB <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">6</span>, <span class="lg-highlight-number">0</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.clearJumboFlag" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
clearJumboFlag
</code>
</span>
</a>
command specifies the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-bounds" target="_self">
<span>
bounds
</span>
</a>
of the
<code class="leafygreen-ui-1l06pbn">
{ "x" : 1 } -->> { "x" :
2 }
</code>
chunk:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n clearJumboFlag: \"test.jumbo\",\n bounds: [{ \"x\" : 1 }, { \"x\" : 2 }]\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clearJumboFlag</span>: <span class="lg-highlight-string">"test.jumbo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bounds</span>: <!-- -->[<!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">2</span> }]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Upon success, the command returns
<code class="leafygreen-ui-1l06pbn">
"ok": 1
</code>
in its output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"ok\" : 1,\n \"operationTime\" : Timestamp(1580190080, 5),\n \"$clusterTime\" : {\n \"clusterTime\" : Timestamp(1580190080, 5),\n \"signature\" : {\n \"hash\" : BinData(0,\"0cYT49s72MHUYV1F2WpoEwlyeVs=\"),\n \"keyId\" : NumberLong(\"6786859092951433239\")\n }\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"operationTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1580190080</span>, <span class="lg-highlight-number">5</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$clusterTime"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clusterTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1580190080</span>, <span class="lg-highlight-number">5</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"signature"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hash"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">BinData</span>(<span class="lg-highlight-number">0</span>,<span class="lg-highlight-string">"0cYT49s72MHUYV1F2WpoEwlyeVs="</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"keyId"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"6786859092951433239"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.clearJumboFlag" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
clearJumboFlag
</code>
</span>
</a>
command specifies the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-bounds" target="_self">
<span>
find
</span>
</a>
field to find the chunk that
contains the shard key
<code class="leafygreen-ui-1l06pbn">
{ "x" : 2 }
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n clearJumboFlag: \"test.jumbo\",\n find: { \"x\" : 2 }\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clearJumboFlag</span>: <span class="lg-highlight-string">"test.jumbo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">find</span>: <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">2</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Upon success, the command returns
<code class="leafygreen-ui-1l06pbn">
"ok": 1
</code>
in its output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"ok\" : 1,\n \"operationTime\" : Timestamp(1580191819, 5),\n \"$clusterTime\" : {\n \"clusterTime\" : Timestamp(1580191819, 5),\n \"signature\" : {\n \"hash\" : BinData(0,\"N6x6drN7HUq5MR5ezUJns1rfeqY=\"),\n \"keyId\" : NumberLong(\"6786859092951433239\")\n }\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"operationTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1580191819</span>, <span class="lg-highlight-number">5</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$clusterTime"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clusterTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1580191819</span>, <span class="lg-highlight-number">5</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"signature"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hash"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">BinData</span>(<span class="lg-highlight-number">0</span>,<span class="lg-highlight-string">"N6x6drN7HUq5MR5ezUJns1rfeqY="</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"keyId"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"6786859092951433239"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To verify the operation, run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-method-sh.status">
<code class="leafygreen-ui-1l06pbn">
sh.status()
</code>
</a>
again. The
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
flag should no longer appear in its output.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"... // Content omitted for brevity\n\ntest.jumbo\n shard key: { \"x\" : 1 }\n unique: false\n balancing: true\n chunks:\n shardA 2\n shardB 2\n { \"x\" : { \"$minKey\" : 1 } } -->> { \"x\" : 1 } on : shardB Timestamp(3, 0)\n { \"x\" : 1 } -->> { \"x\" : 2 } on : shardA Timestamp(7, 0)\n { \"x\" : 2 } -->> { \"x\" : 3 } on : shardA Timestamp(8, 0)\n { \"x\" : 3 } -->> { \"x\" : { \"$maxKey\" : 1 } } on : shardB Timestamp(6, 0)","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">...<!-- --> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Content omitted for </span><span class="lg-highlight-comment">brevity</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">test.<span class="lg-highlight-property">jumbo</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shard <span class="lg-highlight-attr">key</span>: <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unique</span>: <span class="lg-highlight-literal">false</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">balancing</span>: <span class="lg-highlight-literal">true</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">chunks</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->shardA <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shardB <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"$minKey"</span> <!-- -->: <span class="lg-highlight-number">1</span> } } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">1</span> } on <!-- -->: <!-- -->shardB <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">3</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">1</span> } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">2</span> } on <!-- -->: <!-- -->shardA <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">7</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">2</span> } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">3</span> } on <!-- -->: <!-- -->shardA <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">8</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-number">3</span> } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"$maxKey"</span> <!-- -->: <span class="lg-highlight-number">1</span> } } on <!-- -->: <!-- -->shardB <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">6</span>, <span class="lg-highlight-number">0</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Clear Jumbo Flag for a Chunk (Hashed Shard Key)
<a class="headerlink leafygreen-ui-1kepx47" href="#clear-jumbo-flag-for-a-chunk--hashed-shard-key-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="clear-jumbo-flag-for-a-chunk--hashed-shard-key-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-method-sh.status">
<code class="leafygreen-ui-1l06pbn">
sh.status()
</code>
</a>
includes the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-data-sh.status.databases.-collection-.chunk-details">
<code class="leafygreen-ui-1l06pbn">
sh.status.databases.<collection>.chunk-details
</code>
</a>
for the
<code class="leafygreen-ui-1l06pbn">
test.jumboHashed
</code>
collection. The collection uses a hashed shard
key.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"... // Content omitted for brevity\n\ntest.jumboHashed\n shard key: { \"x\" : \"hashed\" }\n unique: false\n balancing: true\n chunks:\n shardA 2\n shardB 2\n { \"x\" : { \"$minKey\" : 1 } } -->> { \"x\" : NumberLong(0) } on : shardA Timestamp(1, 0)\n { \"x\" : NumberLong(0) } -->> { \"x\" : NumberLong(\"848411777775835583\") } on : shardA Timestamp(4, 0)\n { \"x\" : NumberLong(\"848411777775835583\") } -->> { \"x\" : NumberLong(\"5902408780260971510\") } on : shardB Timestamp(4, 1) jumbo\n { \"x\" : NumberLong(\"5902408780260971510\") } -->> { \"x\" : { \"$maxKey\" : 1 } } on : shardB Timestamp(2, 2)","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">...<!-- --> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Content omitted for </span><span class="lg-highlight-comment">brevity</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">test.<span class="lg-highlight-property">jumboHashed</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shard <span class="lg-highlight-attr">key</span>: <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-string">"hashed"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unique</span>: <span class="lg-highlight-literal">false</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">balancing</span>: <span class="lg-highlight-literal">true</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">chunks</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->shardA <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shardB <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"$minKey"</span> <!-- -->: <span class="lg-highlight-number">1</span> } } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>) } on <!-- -->: <!-- -->shardA <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>) } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"848411777775835583"</span>) } on <!-- -->: <!-- -->shardA <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">4</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"848411777775835583"</span>) } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"5902408780260971510"</span>) } on <!-- -->: <!-- -->shardB <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">4</span>, <span class="lg-highlight-number">1</span>) jumbo</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"5902408780260971510"</span>) } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"$maxKey"</span> <!-- -->: <span class="lg-highlight-number">1</span> } } on <!-- -->: <!-- -->shardB <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">2</span>, <span class="lg-highlight-number">2</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To clear the
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
flag for a chunk if the collection uses a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-hashed-shard-key">
hashed shard key
</a>
, use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.clearJumboFlag" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
clearJumboFlag
</code>
</span>
</a>
with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-clearJumboFlag-bounds" target="_self">
<span>
bounds
</span>
</a>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n clearJumboFlag: \"test.jumboHashed\",\n bounds: [{ \"x\" : NumberLong(\"848411777775835583\") }, { \"x\" : NumberLong(\"5902408780260971510\") }]\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clearJumboFlag</span>: <span class="lg-highlight-string">"test.jumboHashed"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bounds</span>: <!-- -->[<!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"848411777775835583"</span>) }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"5902408780260971510"</span>) }]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Upon success, the command returns
<code class="leafygreen-ui-1l06pbn">
"ok": 1
</code>
in its output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"ok\" : 1,\n \"operationTime\" : Timestamp(1580194290, 5),\n \"$clusterTime\" : {\n \"clusterTime\" : Timestamp(1580194290, 5),\n \"signature\" : {\n \"hash\" : BinData(0,\"nWCqOYVrab7NEGHWoo2NYENqHR4=\"),\n \"keyId\" : NumberLong(\"6786875525496307742\")\n }\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"operationTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1580194290</span>, <span class="lg-highlight-number">5</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$clusterTime"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clusterTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1580194290</span>, <span class="lg-highlight-number">5</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"signature"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hash"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">BinData</span>(<span class="lg-highlight-number">0</span>,<span class="lg-highlight-string">"nWCqOYVrab7NEGHWoo2NYENqHR4="</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"keyId"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"6786875525496307742"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To verify the operation, run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-method-sh.status">
<code class="leafygreen-ui-1l06pbn">
sh.status()
</code>
</a>
again. The
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
flag should no longer appear in its output.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"... // Content omitted for brevity\n\ntest.jumboHashed\n shard key: { \"x\" : \"hashed\" }\n unique: false\n balancing: true\n chunks:\n shardA 2\n shardB 2\n { \"x\" : { \"$minKey\" : 1 } } -->> { \"x\" : NumberLong(0) } on : shardA Timestamp(1, 0)\n { \"x\" : NumberLong(0) } -->> { \"x\" : NumberLong(\"848411777775835583\") } on : shardA Timestamp(4, 0)\n { \"x\" : NumberLong(\"848411777775835583\") } -->> { \"x\" : NumberLong(\"5902408780260971510\") } on : shardB Timestamp(5, 0)\n { \"x\" : NumberLong(\"5902408780260971510\") } -->> { \"x\" : { \"$maxKey\" : 1 } } on : shardB Timestamp(2, 2)","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">...<!-- --> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Content omitted for </span><span class="lg-highlight-comment">brevity</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">test.<span class="lg-highlight-property">jumboHashed</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shard <span class="lg-highlight-attr">key</span>: <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-string">"hashed"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unique</span>: <span class="lg-highlight-literal">false</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">balancing</span>: <span class="lg-highlight-literal">true</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">chunks</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->shardA <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shardB <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"$minKey"</span> <!-- -->: <span class="lg-highlight-number">1</span> } } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>) } on <!-- -->: <!-- -->shardA <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>) } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"848411777775835583"</span>) } on <!-- -->: <!-- -->shardA <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">4</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"848411777775835583"</span>) } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"5902408780260971510"</span>) } on <!-- -->: <!-- -->shardB <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">5</span>, <span class="lg-highlight-number">0</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"5902408780260971510"</span>) } <!-- -->-<!-- -->-<!-- -->>> <!-- -->{ <span class="lg-highlight-string">"x"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"$maxKey"</span> <!-- -->: <span class="lg-highlight-number">1</span> } } on <!-- -->: <!-- -->shardB <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<span class="lg-highlight-number">2</span>, <span class="lg-highlight-number">2</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/clear-jumbo-flag/">
Clear
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
Flag
</a>
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/KeyVault.getKey/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
KeyVault.getKey()
<a class="headerlink leafygreen-ui-1kepx47" href="#keyvault.getkey--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="keyvault.getkey--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
KeyVault.getKey(UUID)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-KeyVault.getKey" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-KeyVault.getKey">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Gets a data encryption key with the specified
<code class="leafygreen-ui-1l06pbn">
UUID
</code>
. The data
encryption key must exist in the key vault associated to the database
connection.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-KeyVault.getKey" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
getKey()
</code>
</span>
</a>
has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"keyVault = db.getMongo().getKeyVault()\n\nkeyVault.getKey(UUID(\"<uuid>\"))"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">keyVault = db.getMongo().getKeyVault()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">keyVault.getKey(UUID("<UUID String>"))</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<span class="leafygreen-ui-vm4wms" tabindex="0">
UUID
</span>
is a BSON
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Binary">
<code class="leafygreen-ui-1l06pbn">
binary data
</code>
</a>
object with subtype
<code class="leafygreen-ui-1l06pbn">
4
</code>
.
</p>
<table class="css-11h39yh e1nex8bq0">
<colgroup>
<col class="field-name"/>
<col class="field-body"/>
</colgroup>
<tbody>
<tr class="css-x8snli">
<th>
Returns
<!-- -->
:
</th>
<td>
Document representing a matching data encryption key.
</td>
</tr>
</tbody>
</table>
</dd>
</dl>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Requires Configuring Client-Side Field Level Encryption on Database Connection
<a class="headerlink leafygreen-ui-1kepx47" href="#requires-configuring-client-side-field-level-encryption-on-database-connection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="requires-configuring-client-side-field-level-encryption-on-database-connection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
client-side field level encryption methods
require a database connection with client-side field level encryption
enabled. If the current database connection was not initiated with
client-side field level encryption enabled, either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo/#mongodb-method-Mongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
</a>
constructor from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
to establish a connection with the required client-side field
level encryption options. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo/#mongodb-method-Mongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
</a>
method supports the
following Key Management Service (KMS) providers for Customer
Master Key (CMK) management:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-aws">
Amazon Web Services KMS
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-azure">
Azure Key Vault
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-gcp">
Google Cloud Platform KMS
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-local">
Locally Managed Key
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<em>
or
</em>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-label-mongosh-client-side-field-level-encryption-options" target="_self">
<span>
command line options
</span>
</a>
to establish a
connection with the required options. The command line options only
support the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-aws">
Amazon Web Services KMS
</a>
provider for CMK management.
</p>
</li>
</ul>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following example uses a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers-local">
locally managed KMS
</a>
for the client-side field level
encryption configuration.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Start the mongosh client.","name":"Start mongosh"},{"@type":"HowToStep","text":"To configure client-side field level encryption for a locally\nmanaged key, generate a base64-encoded 96-byte string with no line\nbreaks.","name":"Generate Your Key"},{"@type":"HowToStep","text":"Create the client-side field level encryption options using the\ngenerated local key string:","name":"Create the Client-Side Field Level Encryption Options"},{"@type":"HowToStep","text":"Use the Mongo() constructor with the client-side field level\nencryption options configured to create a database connection. Replace\nthe mongodb://myMongo.example.net URI with the connection\nstring URI of the target cluster.","name":"Create Your Encrypted Client"}],"name":"Example","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start mongosh
<a class="headerlink leafygreen-ui-1kepx47" href="#start-mongosh" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-mongosh">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Start the
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
client.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --nodb","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongosh --nodb</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Generate Your Key
<a class="headerlink leafygreen-ui-1kepx47" href="#generate-your-key" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="generate-your-key">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To configure client-side field level encryption for a locally
managed key, generate a base64-encoded 96-byte string with no line
breaks.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const TEST_LOCAL_KEY = require(\"crypto\").randomBytes(96).toString(\"base64\")","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> <span class="lg-highlight-variable lg-highlight-constant">TEST_LOCAL_KEY</span> <!-- -->= <span class="lg-highlight-built_in">require</span>(<span class="lg-highlight-string">"crypto"</span>).<span class="lg-highlight-title lg-highlight-function">randomBytes</span>(<span class="lg-highlight-number">96</span>).<span class="lg-highlight-title lg-highlight-function">toString</span>(<span class="lg-highlight-string">"base64"</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create the Client-Side Field Level Encryption Options
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-client-side-field-level-encryption-options" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-the-client-side-field-level-encryption-options">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create the client-side field level encryption options using the
generated local key string:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" var autoEncryptionOpts = {\n \"keyVaultNamespace\" : \"encryption.__dataKeys\",\n \"kmsProviders\" : {\n \"local\" : {\n \"key\" : BinData(0, TEST_LOCAL_KEY)\n }\n }\n }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> autoEncryptionOpts <!-- -->= <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"keyVaultNamespace"</span> <!-- -->: <span class="lg-highlight-string">"encryption.__dataKeys"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"kmsProviders"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"local"</span> <!-- -->: <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"key"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">BinData</span>(<span class="lg-highlight-number">0</span>, <span class="lg-highlight-variable lg-highlight-constant">TEST_LOCAL_KEY</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
4
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create Your Encrypted Client
<a class="headerlink leafygreen-ui-1kepx47" href="#create-your-encrypted-client" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-your-encrypted-client">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo/#mongodb-method-Mongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
</a>
constructor with the client-side field level
encryption options configured to create a database connection. Replace
the
<code class="leafygreen-ui-1l06pbn">
mongodb://myMongo.example.net
</code>
URI with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/connection-string/#std-label-mongodb-uri">
connection
string URI
</a>
of the target cluster.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"encryptedClient = Mongo(\n \"mongodb://myMongo.example.net:27017/?replSetName=myMongo\",\n autoEncryptionOpts\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">encryptedClient <!-- -->= <span class="lg-highlight-title lg-highlight-class">Mongo</span>(</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mongodb://myMongo.example.net:27017/?replSetName=myMongo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->autoEncryptionOpts</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Retrieve the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/getKeyVault/#mongodb-method-getKeyVault">
<code class="leafygreen-ui-1l06pbn">
keyVault
</code>
</a>
object and
use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-KeyVault.getKey" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
KeyVault.getKey()
</code>
</span>
</a>
to retrieve
a data encryption key using its
<code class="leafygreen-ui-1l06pbn">
UUID
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"keyVault = encryptedClient.getKeyVault()\nkeyVault.getKey(UUID(\"b4b41b33-5c97-412e-a02b-743498346079\"))","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">keyVault <!-- -->= <!-- -->encryptedClient.<span class="lg-highlight-title lg-highlight-function">getKeyVault</span>(<!-- -->)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">keyVault.<span class="lg-highlight-title lg-highlight-function">getKey</span>(<span class="lg-highlight-title lg-highlight-function">UUID</span>(<span class="lg-highlight-string">"b4b41b33-5c97-412e-a02b-743498346079"</span>))</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-KeyVault.getKey" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
getKey()
</code>
</span>
</a>
returns the data encryption key, with
output similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : UUID(\"b4b41b33-5c97-412e-a02b-743498346079\"),\n \"keyMaterial\" : BinData(0,\"E+0jZKzA4YuE1lGmSVIy2mivqH4JxFo0yFATdxYX/s0YtMFsgVXyu7Bbn4IQ2gn7F/9JAPJFOxdQc5lN3AR+oX33ewVZsd63f3DN1zzcukqdR2Y+EeO7ekRxyRjdzMaNNrBNIv9Gn5LEJgWPSYkG8VczF7cNZnc1YmnR0tuDPNYfm0J7dCZuZUNWW3FCGRcdFx6AlXiCtXKNR97hJ216pQ==\"),\n \"creationDate\" : ISODate(\"2021-03-16T18:22:43.733Z\"),\n \"updateDate\" : ISODate(\"2021-03-16T18:22:43.733Z\"),\n \"status\" : 0, \"version\" : NumberLong(0),\n \"masterKey\" : {\n \"provider\" : \"local\"\n },\n \"keyAltNames\" : [\n \"alpha\"\n ]\n}","programmingLanguage":"JSON"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"_id"</span> <span class="lg-highlight-punctuation">:</span> UUID(<span class="lg-highlight-string">"b4b41b33-5c97-412e-a02b-743498346079"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"keyMaterial"</span> <span class="lg-highlight-punctuation">:</span> BinData(<span class="lg-highlight-number">0</span><span class="lg-highlight-punctuation">,</span><span class="lg-highlight-string">"E+0jZKzA4YuE1lGmSVIy2mivqH4JxFo0yFATdxYX/s0YtMFsgVXyu7Bbn4IQ2gn7F/9JAPJFOxdQc5lN3AR+oX33ewVZsd63f3DN1zzcukqdR2Y+EeO7ekRxyRjdzMaNNrBNIv9Gn5LEJgWPSYkG8VczF7cNZnc1YmnR0tuDPNYfm0J7dCZuZUNWW3FCGRcdFx6AlXiCtXKNR97hJ216pQ=="</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"creationDate"</span> <span class="lg-highlight-punctuation">:</span> ISODate(<span class="lg-highlight-string">"2021-03-16T18:22:43.733Z"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"updateDate"</span> <span class="lg-highlight-punctuation">:</span> ISODate(<span class="lg-highlight-string">"2021-03-16T18:22:43.733Z"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"status"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">0</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-attr">"version"</span> <span class="lg-highlight-punctuation">:</span> NumberLong(<span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"masterKey"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"provider"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"local"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"keyAltNames"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"alpha"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">]</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/compatibility/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#queryable-encryption-compatibility" target="_self">
<span>
Queryable Encryption Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#queryable-encryption-driver-compatibility" target="_self">
<span>
Queryable Encryption Driver Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#mongodb-support-limitations" target="_self">
<span>
MongoDB Support Limitations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#client-side-field-level-encryption-compatibility" target="_self">
<span>
Client-Side Field Level Encryption Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#client-side-field-level-encryption-driver-compatibility" target="_self">
<span>
Client-Side Field Level Encryption Driver Compatibility
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
This page describes the MongoDB editions and driver versions compatible
with Queryable Encryption and Client-Side Field Level Encryption to help you determine whether your deployment
supports each in-use encryption feature.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-compatibility-reference">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Queryable Encryption Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#queryable-encryption-compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="queryable-encryption-compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can use Queryable Encryption on a MongoDB 7.0 or later replica set or sharded
cluster, but not a standalone instance. The following table shows which
MongoDB server products support which Queryable Encryption mechanisms:
</p>
<div class="header-buffer" id="ref-1-id1">
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:15%"/>
<col style="width:30%"/>
<col style="width:30%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Product Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Minimum Version
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Supports Queryable Encryption with Automatic Encryption
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Supports Queryable Encryption with Explicit Encryption
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB Atlas
<a class="footnote-reference header-buffer" href="#footnote-1" id="ref-1-id1">
[
<!-- -->
1
<!-- -->
]
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
7.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB Enterprise Advanced
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
7.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB Community Edition
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
7.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-1" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-1-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
Queryable Encryption is compatible with MongoDB Atlas but not
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
<span>
MongoDB Atlas Search
<!-- -->
.
</span>
</a>
</td>
</tr>
</tbody>
</table>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-driver-compatibility">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Queryable Encryption Driver Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#queryable-encryption-driver-compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="queryable-encryption-driver-compatibility">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption requires the following minimum versions for
compatible MongoDB drivers:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:25%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Driver
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Minimum Version
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Encryption Library
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/c/" target="_self">
<span>
C
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1.24.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/reference/libmongocrypt/#std-label-qe-reference-libmongocrypt">
libmongocrypt
</a>
version 1.8.0 or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/cxx/" target="_self">
<span>
C++
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
3.8.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/reference/libmongocrypt/#std-label-qe-reference-libmongocrypt">
libmongocrypt
</a>
version 1.8.0 or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/csharp/" target="_self">
<span>
C#/.NET
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2.20.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
No additional dependency for C#/.NET versions earlier than 3.0.
</p>
<p class="leafygreen-ui-1kp3ins">
C#/.NET version 3.0 or later requires the
<code class="leafygreen-ui-1l06pbn">
MongoDB.Driver.Encryption
</code>
package.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/go/" target="_self">
<span>
Go
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1.12
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/reference/libmongocrypt/#std-label-qe-reference-libmongocrypt">
libmongocrypt
</a>
version 1.8.0
or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/java/sync/" target="_self">
<span>
Java Sync
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/languages/java/reactive-streams-driver/current/" target="_self">
<span>
Java Reactive
Streams
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
4.10.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mvnrepository.com/artifact/org.mongodb/mongodb-crypt" rel="noopener noreferrer">
<span>
mongodb-crypt
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
version 1.8.0 or later
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/node/" target="_self">
<span>
Node.js
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
5.5.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.npmjs.com/package/mongodb-client-encryption/" rel="noopener noreferrer">
<span>
mongodb-client-encryption
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
version 2.8.0 or later.
</p>
<p class="leafygreen-ui-1kp3ins">
Node 6.0.0 or later requires
<code class="leafygreen-ui-1l06pbn">
mongodb-client-encryption
</code>
with the
same major version number as the Node.js driver. For example,
Node.js driver v6.x.x requires
<code class="leafygreen-ui-1l06pbn">
mongodb-client-encryption
</code>
v6.x.x.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/php/" target="_self">
<span>
PHP
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1.16
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No additional dependency.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/python/" target="_self">
<span>
PyMongo
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
4.4
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://pypi.org/project/pymongocrypt/" rel="noopener noreferrer">
<span>
pymongocrypt
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
version
1.6 or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/ruby/" target="_self">
<span>
Ruby
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2.19
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://rubygems.org/gems/libmongocrypt-helper/" rel="noopener noreferrer">
<span>
libmongocrypt-helper
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
version 1.8.0 or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/rust/" target="_self">
<span>
Rust
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2.4.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/reference/libmongocrypt/#std-label-qe-reference-libmongocrypt">
libmongocrypt
</a>
version 1.8.0
or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/scala/" target="_self">
<span>
Scala
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
4.10.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mvnrepository.com/artifact/org.mongodb/mongodb-crypt" rel="noopener noreferrer">
<span>
mongodb-crypt
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
version 1.8.0 or later
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
MongoDB Support Limitations
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-support-limitations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-support-limitations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Enabling Queryable Encryption on a collection redacts fields from some diagnostic
commands and omits some operations from the query log. This limits the
data available to MongoDB support engineers, especially when
analyzing query performance. To measure the impact of operations against
encrypted collections, use a third party application performance
monitoring tool to collect metrics.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-csfle-compatibility-reference">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Client-Side Field Level Encryption Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#client-side-field-level-encryption-compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="client-side-field-level-encryption-compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can use Client-Side Field Level Encryption (CSFLE) replica set or sharded cluster, but not
a standalone instance. The following table shows which MongoDB server products
support which Client-Side Field Level Encryption mechanisms:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:15%"/>
<col style="width:30%"/>
<col style="width:30%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Product Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Minimum Version
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Supports CSFLE with Automatic Encryption
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Supports CSFLE with Explicit Encryption
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB Atlas
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/reference/faq/database/#which-versions-of-mongodb-do-service-clusters-use-" target="_self">
<span>
All supported MongoDB versions
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB Enterprise Advanced
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
4.2
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB Community Edition
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
4.2
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Yes
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-csfle-driver-compatibility">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Client-Side Field Level Encryption Driver Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#client-side-field-level-encryption-driver-compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="client-side-field-level-encryption-driver-compatibility">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Client-Side Field Level Encryption requires the following minimum versions for
compatible MongoDB drivers.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Key Rotation Support
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
To use the Key Rotation API, you must use specific versions
of either your driver's binding package or
<code class="leafygreen-ui-1l06pbn">
libmongocrypt
</code>
.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-csfle-reference-compatibility-key-rotation">
</span>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:25%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Driver
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Minimum Version
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Key Rotation Requirements
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/c/" target="_self">
<span>
C
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1.17.5
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No additional requirements.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/cxx/" target="_self">
<span>
C++
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
3.6.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No additional requirements.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/csharp/" target="_self">
<span>
C#/.NET
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2.10.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Driver version 2.17.1 or later.
</p>
<p class="leafygreen-ui-1kp3ins">
C#/.NET version 3.0 or later requires the
<code class="leafygreen-ui-1l06pbn">
MongoDB.Driver.Encryption
</code>
package.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/go/" target="_self">
<span>
Go
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1.2
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
libmongocrypt
</code>
version 1.5.2 or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/java/sync/" target="_self">
<span>
Java Sync
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
3.11.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
mongodb-crypt
</code>
version 1.7.3 or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/languages/java/reactive-streams-driver/current/" target="_self">
<span>
Java Reactive Streams
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1.12.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
mongodb-crypt
</code>
version 1.7.3 or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/node/" target="_self">
<span>
Node.js
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
3.4.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
For driver version 6.0 or later, use the same
<code class="leafygreen-ui-1l06pbn">
mongodb-client-encryption
</code>
major version as the driver.
Otherwise, use
<code class="leafygreen-ui-1l06pbn">
mongodb-client-encryption
</code>
2.2.0 - 2.x.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/php/" target="_self">
<span>
PHP
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1.6.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No additional requirements.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/pymongo/" target="_self">
<span>
Python (PyMongo)
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
3.10.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
pymongocrypt
</code>
version 1.3.1 or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://docs.mongodb.com/ruby-driver/current/" target="_self">
<span>
Ruby
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2.12.1
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No additional requirements.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/rust/" target="_self">
<span>
Rust
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2.4.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/reference/libmongocrypt/#std-label-csfle-reference-libmongocrypt">
libmongocrypt
</a>
version 1.8.0
or later.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/scala/" target="_self">
<span>
Scala
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2.7.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
No additional requirements.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/inconsistency-type/RoutingTableRangeOverlap/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
RoutingTableRangeOverlap
<a class="headerlink leafygreen-ui-1kepx47" href="#routingtablerangeoverlap" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="routingtablerangeoverlap">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#format" target="_self">
<span>
Format
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
RoutingTableRangeOverlap
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-RoutingTableRangeOverlap" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-RoutingTableRangeOverlap">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The routing table of a collection contains two chunks with overlapping shard key ranges.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Format
<a class="headerlink leafygreen-ui-1kepx47" href="#format" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="format">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n type: \"RoutingTableRangeOverlap\",\n description: \"<string>\",\n details: {\n namespace: \"<string>\",\n collectionUUID: <string>,\n chunkA: {<document>},\n chunkB: {<document>}\n }\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"RoutingTableRangeOverlap"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> description<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"<string>"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> details<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> namespace<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"<string>"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> collectionUUID<span class="lg-highlight-punctuation">:</span> <string><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> chunkA<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span><document><span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> chunkB<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span><document><span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
RoutingTableRangeOverlap
</code>
inconsistency documents contain the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:60%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
type
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Type of inconsistency document. Returns a value of
<code class="leafygreen-ui-1l06pbn">
RoutingTableRangeOverlap
</code>
for this
inconsistency type.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
description
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Describes the inconsistency found in the metadata.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Document with additional information on where the inconsistency exists in the
database and sharded cluster.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.namespace
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Indicates the database and collection where the inconsistency exists.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.collectionUUID
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
UUID
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
UUID of the collection.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.chunkA
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The first chunk that overlaps with the second chunk.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
details.chunkB
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The second chunk that overlaps with the first chunk.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.adminCommand/#mongodb-method-db.adminCommand">
<code class="leafygreen-ui-1l06pbn">
db.adminCommand()
</code>
</a>
method to call the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/checkMetadataConsistency/#mongodb-dbcommand-dbcmd.checkMetadataConsistency">
<code class="leafygreen-ui-1l06pbn">
checkMetadataConsistency
</code>
</a>
command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { checkMetadataConsistency: 1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{ <span class="lg-highlight-attr">checkMetadataConsistency</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The method returns a cursor with a batch of documents showing the inconsistencies
found in the sharding metadata. The example below shows a cursor with
a
<code class="leafygreen-ui-1l06pbn">
RoutingTableRangeOverlap
</code>
inconsistency document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n cursor: {\n id: Long(\"0\"),\n ns: \"test.$cmd.aggregate\",\n firstBatch: [\n {\n type: \"RoutingTableRangeOverlap\",\n description: \"Found two overlapping consecutive chunks\",\n details: {\n namespace: \"test.authors\",\n collectionUUID: new UUID(\"62ebdb7e-a7bb-4151-a620-49d44cef097f\"),\n chunkA: {\n _id: ObjectId(\"64dde749ac6045c92ef3b9ad\"),\n uuid: new UUID(\"8c83ea89-ab6b-4ae4-91ab-813cf781284d\"),\n min: {\n x: MinKey\n },\n max: {\n x: 0\n },\n shard: \"shard0000\",\n lastmod: Timestamp(1, 0),\n onCurrentShardSince: Timestamp(1, 0),\n history: [\n {\n validAfter: Timestamp(1, 0),\n shard: \"shard0000\"\n }\n ]\n },\n chunkB: {\n _id: ObjectId(\"64dde749ac6045c92ef3b9af\"),\n uuid: new UUID(\"8c83ea89-ab6b-4ae4-91ab-813cf781284d\"),\n min: {\n x: -10\n },\n max: {\n x: MaxKey\n },\n shard: \"shard0000\",\n lastmod: Timestamp(1, 0),\n onCurrentShardSince: Timestamp(1, 0),\n history: [\n {\n validAfter: Timestamp(1, 0),\n shard: \"shard0000\"\n }\n ]\n }\n }\n }\n ],\n },\n ok: 1\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> id<span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"0"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ns<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.$cmd.aggregate"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> firstBatch<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> type<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"RoutingTableRangeOverlap"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> description<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"Found two overlapping consecutive chunks"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> details<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> namespace<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.authors"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> collectionUUID<span class="lg-highlight-punctuation">:</span> new UUID(<span class="lg-highlight-string">"62ebdb7e-a7bb-4151-a620-49d44cef097f"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> chunkA<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> _id<span class="lg-highlight-punctuation">:</span> ObjectId(<span class="lg-highlight-string">"64dde749ac6045c92ef3b9ad"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> uuid<span class="lg-highlight-punctuation">:</span> new UUID(<span class="lg-highlight-string">"8c83ea89-ab6b-4ae4-91ab-813cf781284d"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> min<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> x<span class="lg-highlight-punctuation">:</span> MinKey</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> max<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> x<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">0</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> shard<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"shard0000"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> lastmod<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> onCurrentShardSince<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> history<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> validAfter<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> shard<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"shard0000"</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">]</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> chunkB<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> _id<span class="lg-highlight-punctuation">:</span> ObjectId(<span class="lg-highlight-string">"64dde749ac6045c92ef3b9af"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> uuid<span class="lg-highlight-punctuation">:</span> new UUID(<span class="lg-highlight-string">"8c83ea89-ab6b-4ae4-91ab-813cf781284d"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> min<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> x<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">-10</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> max<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> x<span class="lg-highlight-punctuation">:</span> MaxKey</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> shard<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"shard0000"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> lastmod<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> onCurrentShardSince<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> history<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> validAfter<span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> shard<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"shard0000"</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">]</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">]</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ok<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/graphLookup/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$graphLookup (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-graphlookup--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-graphlookup--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#considerations" target="_self">
<span>
Considerations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#additional-resource" target="_self">
<span>
Additional Resource
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="pipeline">
<dt>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-pipeline-pipe.-graphLookup" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-pipeline-pipe.-graphLookup">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.1
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Performs a recursive search on a collection, with options for
restricting the search by recursion depth and query filter.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
search process is summarized below:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Input documents flow into the
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
stage of an
aggregation operation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
targets the search to the collection
designated by the
<code class="leafygreen-ui-1l06pbn">
from
</code>
parameter (see below for full
list of search parameters).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For each input document, the search begins with the value
designated by
<code class="leafygreen-ui-1l06pbn">
startWith
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
matches the
<code class="leafygreen-ui-1l06pbn">
startWith
</code>
value
against the field designated by
<code class="leafygreen-ui-1l06pbn">
connectToField
</code>
in other
documents in the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For each matching document,
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
takes the value of
the
<code class="leafygreen-ui-1l06pbn">
connectFromField
</code>
and checks every document in the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection for a matching
<code class="leafygreen-ui-1l06pbn">
connectToField
</code>
value. For
each match,
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
adds the matching document in the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection to an array field named by the
<code class="leafygreen-ui-1l06pbn">
as
</code>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
This step continues recursively until no more matching documents
are found, or until the operation reaches a recursion depth
specified by the
<code class="leafygreen-ui-1l06pbn">
maxDepth
</code>
parameter.
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
then
appends the array field to the input document.
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
returns results after completing its search on all input
documents.
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
has the following prototype form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $graphLookup: {\n from: <collection>,\n startWith: <expression>,\n connectFromField: <string>,\n connectToField: <string>,\n as: <string>,\n maxDepth: <number>,\n depthField: <string>,\n restrictSearchWithMatch: <document>\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$graphLookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <<!-- -->collection<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startWith</span>: <<!-- -->expression<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">connectFromField</span>: <<!-- -->string<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">connectToField</span>: <<!-- -->string<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <<!-- -->string<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">maxDepth</span>: <<!-- -->number<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">depthField</span>: <<!-- -->string<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restrictSearchWithMatch</span>: <<span class="lg-highlight-variable lg-highlight-language">document</span>></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
takes a document with the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:40%"/>
<col style="width:60%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
from
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Target collection for the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
operation to search, recursively matching the
<code class="leafygreen-ui-1l06pbn">
connectFromField
</code>
to the
<code class="leafygreen-ui-1l06pbn">
connectToField
</code>
.
The
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection must be in the same
database as any other collections used in the operation.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.1, the collection specified in the
<code class="leafygreen-ui-1l06pbn">
from
</code>
parameter can be sharded.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
startWith
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression
</a>
that specifies
the value of the
<code class="leafygreen-ui-1l06pbn">
connectFromField
</code>
with which to start the
recursive search. Optionally,
<code class="leafygreen-ui-1l06pbn">
startWith
</code>
may be array of
values, each of which is individually followed through the
traversal process.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
connectFromField
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Field name whose value
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
uses to
recursively match against the
<code class="leafygreen-ui-1l06pbn">
connectToField
</code>
of other
documents in the collection. If the value is an array, each
element is individually followed through the
traversal process.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
connectToField
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Field name in other documents against which to match the
value of the field specified by the
<code class="leafygreen-ui-1l06pbn">
connectFromField
</code>
parameter.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
as
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Name of the array field added to each output document.
Contains the documents traversed in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
stage to reach the document.
</p>
<p class="leafygreen-ui-1kp3ins">
Documents returned in the
<code class="leafygreen-ui-1l06pbn">
as
</code>
field are not guaranteed to be in
any order.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
maxDepth
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<em>
Optional.
</em>
Non-negative integral number specifying the
maximum recursion depth.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
depthField
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<em>
Optional.
</em>
Name of the field to add to each traversed
document in the search path. The value of this field
is the recursion depth for the document, represented as a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json-v1/#mongodb-bsontype-data_numberlong">
<code class="leafygreen-ui-1l06pbn">
NumberLong
</code>
</a>
. Recursion depth
value starts at zero, so the first lookup corresponds to
zero depth.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
restrictSearchWithMatch
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
<em>
Optional.
</em>
A document specifying additional conditions
for the recursive search. The syntax is identical to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-documents/#std-label-read-operations-query-argument">
query filter
</a>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot use any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
aggregation expression
</a>
in this filter. For example, you can't use
the following document to find documents in which the
<code class="leafygreen-ui-1l06pbn">
lastName
</code>
value is different from the
<code class="leafygreen-ui-1l06pbn">
lastName
</code>
value of the input document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ lastName: { $ne: \"$lastName\" } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">lastName</span>: <!-- -->{ <span class="lg-highlight-attr">$ne</span>: <span class="lg-highlight-string">"$lastName"</span> } }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You can't use the document in this context, because
<code class="leafygreen-ui-1l06pbn">
"$lastName"
</code>
will act as a string literal, not a field path.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="considerations">
</div>
</a>
</h2>
<span class="leafygreen-ui-1qnjd6d" id="std-label-graphLookup-sharded-collections">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sharded Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#sharded-collections" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sharded-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.1, you can specify
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharding-sharded-cluster">
sharded collections
</a>
in the
<code class="leafygreen-ui-1l06pbn">
from
</code>
parameter of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
stages.
</p>
<p class="leafygreen-ui-1kp3ins">
You
<strong>
cannot
</strong>
use the
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
stage within a transaction while
targeting a sharded collection.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Max Depth
<a class="headerlink leafygreen-ui-1kepx47" href="#max-depth" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="max-depth">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Setting the
<code class="leafygreen-ui-1l06pbn">
maxDepth
</code>
field to
<code class="leafygreen-ui-1l06pbn">
0
</code>
is equivalent to a
non-recursive
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
search stage.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Memory
<a class="headerlink leafygreen-ui-1kepx47" href="#memory" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="memory">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
stage must stay within the 100 megabyte
memory limit. If
<code class="leafygreen-ui-1l06pbn">
allowDiskUse: true
</code>
is specified for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate()
</code>
</a>
operation, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
stage ignores the option. If there are other
stages in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate()
</code>
</a>
operation,
<code class="leafygreen-ui-1l06pbn">
allowDiskUse: true
</code>
option is in effect for these other stages.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline-limits/">
aggregration pipeline limitations
</a>
for more information.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Unsorted Results
<a class="headerlink leafygreen-ui-1kepx47" href="#unsorted-results" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="unsorted-results">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
stage does not return sorted results. To sort your results,
use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sortArray/#mongodb-expression-exp.-sortArray">
<code class="leafygreen-ui-1l06pbn">
$sortArray
</code>
</a>
operator.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Views and Collation
<a class="headerlink leafygreen-ui-1kepx47" href="#views-and-collation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="views-and-collation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If performing an aggregation that involves multiple views, such as
with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
, the views must
have the same
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Within a Single Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#within-a-single-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="within-a-single-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
A collection named
<code class="leafygreen-ui-1l06pbn">
employees
</code>
has the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"name\" : \"Dev\" }\n{ \"_id\" : 2, \"name\" : \"Eliot\", \"reportsTo\" : \"Dev\" }\n{ \"_id\" : 3, \"name\" : \"Ron\", \"reportsTo\" : \"Eliot\" }\n{ \"_id\" : 4, \"name\" : \"Andrew\", \"reportsTo\" : \"Eliot\" }\n{ \"_id\" : 5, \"name\" : \"Asya\", \"reportsTo\" : \"Ron\" }\n{ \"_id\" : 6, \"name\" : \"Dan\", \"reportsTo\" : \"Andrew\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Ron"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Andrew"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Asya"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Ron"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dan"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Andrew"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
operation recursively matches
on the
<code class="leafygreen-ui-1l06pbn">
reportsTo
</code>
and
<code class="leafygreen-ui-1l06pbn">
name
</code>
fields in the
<code class="leafygreen-ui-1l06pbn">
employees
</code>
collection, returning the reporting hierarchy for each person:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.employees.aggregate( [\n {\n $graphLookup: {\n from: \"employees\",\n startWith: \"$reportsTo\",\n connectFromField: \"reportsTo\",\n connectToField: \"name\",\n as: \"reportingHierarchy\"\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">employees</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$graphLookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"employees"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startWith</span>: <span class="lg-highlight-string">"$reportsTo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">connectFromField</span>: <span class="lg-highlight-string">"reportsTo"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">connectToField</span>: <span class="lg-highlight-string">"name"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"reportingHierarchy"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The output resembles the following results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1,\n \"name\" : \"Dev\",\n \"reportingHierarchy\" : [ ]\n}\n{\n \"_id\" : 2,\n \"name\" : \"Eliot\",\n \"reportsTo\" : \"Dev\",\n \"reportingHierarchy\" : [\n { \"_id\" : 1, \"name\" : \"Dev\" }\n ]\n}\n{\n \"_id\" : 3,\n \"name\" : \"Ron\",\n \"reportsTo\" : \"Eliot\",\n \"reportingHierarchy\" : [\n { \"_id\" : 2, \"name\" : \"Eliot\", \"reportsTo\" : \"Dev\" },\n { \"_id\" : 1, \"name\" : \"Dev\" }\n ]\n}\n{\n \"_id\" : 4,\n \"name\" : \"Andrew\",\n \"reportsTo\" : \"Eliot\",\n \"reportingHierarchy\" : [\n { \"_id\" : 2, \"name\" : \"Eliot\", \"reportsTo\" : \"Dev\" },\n { \"_id\" : 1, \"name\" : \"Dev\" }\n ]\n}\n{\n \"_id\" : 5,\n \"name\" : \"Asya\",\n \"reportsTo\" : \"Ron\",\n \"reportingHierarchy\" : [\n { \"_id\" : 2, \"name\" : \"Eliot\", \"reportsTo\" : \"Dev\" },\n { \"_id\" : 3, \"name\" : \"Ron\", \"reportsTo\" : \"Eliot\" },\n { \"_id\" : 1, \"name\" : \"Dev\" }\n ]\n}\n{\n \"_id\" : 6,\n \"name\" : \"Dan\",\n \"reportsTo\" : \"Andrew\",\n \"reportingHierarchy\" : [\n { \"_id\" : 4, \"name\" : \"Andrew\", \"reportsTo\" : \"Eliot\" },\n { \"_id\" : 2, \"name\" : \"Eliot\", \"reportsTo\" : \"Dev\" },\n { \"_id\" : 1, \"name\" : \"Dev\" }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportingHierarchy"</span> <!-- -->: <!-- -->[ <!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportingHierarchy"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Ron"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportingHierarchy"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Andrew"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportingHierarchy"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Asya"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Ron"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportingHierarchy"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Ron"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">6</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dan"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Andrew"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reportingHierarchy"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Andrew"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table provides a traversal path for the
document
<code class="leafygreen-ui-1l06pbn">
{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" }
</code>
:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:15%"/>
<col style="width:85%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
Start value
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
reportsTo
</code>
value of the document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ ... \"reportsTo\" : \"Ron\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->...<!-- --> <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Ron"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
Depth 0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 3, \"name\" : \"Ron\", \"reportsTo\" : \"Eliot\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Ron"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
Depth 1
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 2, \"name\" : \"Eliot\", \"reportsTo\" : \"Dev\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>, <span class="lg-highlight-string">"reportsTo"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
Depth 2
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"name\" : \"Dev\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The output generates the hierarchy
<code class="leafygreen-ui-1l06pbn">
Asya -> Ron -> Eliot -> Dev
</code>
.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Across Multiple Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#across-multiple-collections" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="across-multiple-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Like
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
can access
another collection in the same database.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, create a database with two collections:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An
<code class="leafygreen-ui-1l06pbn">
airports
</code>
collection with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.airports.insertMany( [\n { \"_id\" : 0, \"airport\" : \"JFK\", \"connects\" : [ \"BOS\", \"ORD\" ] },\n { \"_id\" : 1, \"airport\" : \"BOS\", \"connects\" : [ \"JFK\", \"PWM\" ] },\n { \"_id\" : 2, \"airport\" : \"ORD\", \"connects\" : [ \"JFK\" ] },\n { \"_id\" : 3, \"airport\" : \"PWM\", \"connects\" : [ \"BOS\", \"LHR\" ] },\n { \"_id\" : 4, \"airport\" : \"LHR\", \"connects\" : [ \"PWM\" ] }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">airports</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"ORD"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span>, <span class="lg-highlight-string">"PWM"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"ORD"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"PWM"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"LHR"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"LHR"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"PWM"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
travelers
</code>
collection with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.travelers.insertMany( [\n { \"_id\" : 1, \"name\" : \"Dev\", \"nearestAirport\" : \"JFK\" },\n { \"_id\" : 2, \"name\" : \"Eliot\", \"nearestAirport\" : \"JFK\" },\n { \"_id\" : 3, \"name\" : \"Jeff\", \"nearestAirport\" : \"BOS\" }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">travelers</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span>, <span class="lg-highlight-string">"nearestAirport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>, <span class="lg-highlight-string">"nearestAirport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Jeff"</span>, <span class="lg-highlight-string">"nearestAirport"</span> <!-- -->: <span class="lg-highlight-string">"BOS"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For each document in the
<code class="leafygreen-ui-1l06pbn">
travelers
</code>
collection, the following
aggregation operation looks up the
<code class="leafygreen-ui-1l06pbn">
nearestAirport
</code>
value in the
<code class="leafygreen-ui-1l06pbn">
airports
</code>
collection and recursively matches the
<code class="leafygreen-ui-1l06pbn">
connects
</code>
field to the
<code class="leafygreen-ui-1l06pbn">
airport
</code>
field. The operation specifies a maximum
recursion depth of
<code class="leafygreen-ui-1l06pbn">
2
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.travelers.aggregate( [\n {\n $graphLookup: {\n from: \"airports\",\n startWith: \"$nearestAirport\",\n connectFromField: \"connects\",\n connectToField: \"airport\",\n maxDepth: 2,\n depthField: \"numConnections\",\n as: \"destinations\"\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">travelers</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$graphLookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"airports"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startWith</span>: <span class="lg-highlight-string">"$nearestAirport"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">connectFromField</span>: <span class="lg-highlight-string">"connects"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">connectToField</span>: <span class="lg-highlight-string">"airport"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">maxDepth</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">depthField</span>: <span class="lg-highlight-string">"numConnections"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"destinations"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The output resembles the following results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1,\n \"name\" : \"Dev\",\n \"nearestAirport\" : \"JFK\",\n \"destinations\" : [\n { \"_id\" : 3,\n \"airport\" : \"PWM\",\n \"connects\" : [ \"BOS\", \"LHR\" ],\n \"numConnections\" : NumberLong(2) },\n { \"_id\" : 2,\n \"airport\" : \"ORD\",\n \"connects\" : [ \"JFK\" ],\n \"numConnections\" : NumberLong(1) },\n { \"_id\" : 1,\n \"airport\" : \"BOS\",\n \"connects\" : [ \"JFK\", \"PWM\" ],\n \"numConnections\" : NumberLong(1) },\n { \"_id\" : 0,\n \"airport\" : \"JFK\",\n \"connects\" : [ \"BOS\", \"ORD\" ],\n \"numConnections\" : NumberLong(0) }\n ]\n}\n{\n \"_id\" : 2,\n \"name\" : \"Eliot\",\n \"nearestAirport\" : \"JFK\",\n \"destinations\" : [\n { \"_id\" : 3,\n \"airport\" : \"PWM\",\n \"connects\" : [ \"BOS\", \"LHR\" ],\n \"numConnections\" : NumberLong(2) },\n { \"_id\" : 2,\n \"airport\" : \"ORD\",\n \"connects\" : [ \"JFK\" ],\n \"numConnections\" : NumberLong(1) },\n { \"_id\" : 1,\n \"airport\" : \"BOS\",\n \"connects\" : [ \"JFK\", \"PWM\" ],\n \"numConnections\" : NumberLong(1) },\n { \"_id\" : 0,\n \"airport\" : \"JFK\",\n \"connects\" : [ \"BOS\", \"ORD\" ],\n \"numConnections\" : NumberLong(0) } ]\n}\n{\n \"_id\" : 3,\n \"name\" : \"Jeff\",\n \"nearestAirport\" : \"BOS\",\n \"destinations\" : [\n { \"_id\" : 2,\n \"airport\" : \"ORD\",\n \"connects\" : [ \"JFK\" ],\n \"numConnections\" : NumberLong(2) },\n { \"_id\" : 3,\n \"airport\" : \"PWM\",\n \"connects\" : [ \"BOS\", \"LHR\" ],\n \"numConnections\" : NumberLong(1) },\n { \"_id\" : 4,\n \"airport\" : \"LHR\",\n \"connects\" : [ \"PWM\" ],\n \"numConnections\" : NumberLong(2) },\n { \"_id\" : 0,\n \"airport\" : \"JFK\",\n \"connects\" : [ \"BOS\", \"ORD\" ],\n \"numConnections\" : NumberLong(1) },\n { \"_id\" : 1,\n \"airport\" : \"BOS\",\n \"connects\" : [ \"JFK\", \"PWM\" ],\n \"numConnections\" : NumberLong(0) }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dev"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nearestAirport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"destinations"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"PWM"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"LHR"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">2</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"ORD"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">1</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"BOS"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span>, <span class="lg-highlight-string">"PWM"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">1</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"ORD"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>) }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Eliot"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nearestAirport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"destinations"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"PWM"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"LHR"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">2</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"ORD"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">1</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"BOS"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span>, <span class="lg-highlight-string">"PWM"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">1</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"ORD"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>) } ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Jeff"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nearestAirport"</span> <!-- -->: <span class="lg-highlight-string">"BOS"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"destinations"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"ORD"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">2</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"PWM"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"LHR"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">1</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"LHR"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"PWM"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">2</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"ORD"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">1</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"BOS"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span>, <span class="lg-highlight-string">"PWM"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numConnections"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>) }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table provides a traversal path for the recursive
search, up to depth
<code class="leafygreen-ui-1l06pbn">
2
</code>
, where the starting
<code class="leafygreen-ui-1l06pbn">
airport
</code>
is
<code class="leafygreen-ui-1l06pbn">
JFK
</code>
:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Start value
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
nearestAirport
</code>
value from the
<code class="leafygreen-ui-1l06pbn">
travelers
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ ... \"nearestAirport\" : \"JFK\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->...<!-- --> <span class="lg-highlight-string">"nearestAirport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Depth 0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 0, \"airport\" : \"JFK\", \"connects\" : [ \"BOS\", \"ORD\" ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"JFK"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"ORD"</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Depth 1
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"airport\" : \"BOS\", \"connects\" : [ \"JFK\", \"PWM\" ] }\n{ \"_id\" : 2, \"airport\" : \"ORD\", \"connects\" : [ \"JFK\" ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span>, <span class="lg-highlight-string">"PWM"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"ORD"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"JFK"</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Depth 2
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 3, \"airport\" : \"PWM\", \"connects\" : [ \"BOS\", \"LHR\" ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"airport"</span> <!-- -->: <span class="lg-highlight-string">"PWM"</span>, <span class="lg-highlight-string">"connects"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"BOS"</span>, <span class="lg-highlight-string">"LHR"</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
With a Query Filter
<a class="headerlink leafygreen-ui-1kepx47" href="#with-a-query-filter" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="with-a-query-filter">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example uses a collection with a set
of documents containing names of people along with arrays of their
friends and their hobbies. An aggregation operation finds one
particular person and traverses her network of connections to find
people who list
<code class="leafygreen-ui-1l06pbn">
golf
</code>
among their hobbies.
</p>
<p class="leafygreen-ui-1kp3ins">
A collection named
<code class="leafygreen-ui-1l06pbn">
people
</code>
contains the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1,\n \"name\" : \"Tanya Jordan\",\n \"friends\" : [ \"Shirley Soto\", \"Terry Hawkins\", \"Carole Hale\" ],\n \"hobbies\" : [ \"tennis\", \"unicycling\", \"golf\" ]\n}\n{\n \"_id\" : 2,\n \"name\" : \"Carole Hale\",\n \"friends\" : [ \"Joseph Dennis\", \"Tanya Jordan\", \"Terry Hawkins\" ],\n \"hobbies\" : [ \"archery\", \"golf\", \"woodworking\" ]\n}\n{\n \"_id\" : 3,\n \"name\" : \"Terry Hawkins\",\n \"friends\" : [ \"Tanya Jordan\", \"Carole Hale\", \"Angelo Ward\" ],\n \"hobbies\" : [ \"knitting\", \"frisbee\" ]\n}\n{\n \"_id\" : 4,\n \"name\" : \"Joseph Dennis\",\n \"friends\" : [ \"Angelo Ward\", \"Carole Hale\" ],\n \"hobbies\" : [ \"tennis\", \"golf\", \"topiary\" ]\n}\n{\n \"_id\" : 5,\n \"name\" : \"Angelo Ward\",\n \"friends\" : [ \"Terry Hawkins\", \"Shirley Soto\", \"Joseph Dennis\" ],\n \"hobbies\" : [ \"travel\", \"ceramics\", \"golf\" ]\n}\n{\n \"_id\" : 6,\n \"name\" : \"Shirley Soto\",\n \"friends\" : [ \"Angelo Ward\", \"Tanya Jordan\", \"Carole Hale\" ],\n \"hobbies\" : [ \"frisbee\", \"set theory\" ]\n }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Tanya Jordan"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"friends"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Shirley Soto"</span>, <span class="lg-highlight-string">"Terry Hawkins"</span>, <span class="lg-highlight-string">"Carole Hale"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hobbies"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"tennis"</span>, <span class="lg-highlight-string">"unicycling"</span>, <span class="lg-highlight-string">"golf"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Carole Hale"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"friends"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Joseph Dennis"</span>, <span class="lg-highlight-string">"Tanya Jordan"</span>, <span class="lg-highlight-string">"Terry Hawkins"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hobbies"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"archery"</span>, <span class="lg-highlight-string">"golf"</span>, <span class="lg-highlight-string">"woodworking"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Terry Hawkins"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"friends"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Tanya Jordan"</span>, <span class="lg-highlight-string">"Carole Hale"</span>, <span class="lg-highlight-string">"Angelo Ward"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hobbies"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"knitting"</span>, <span class="lg-highlight-string">"frisbee"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Joseph Dennis"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"friends"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Angelo Ward"</span>, <span class="lg-highlight-string">"Carole Hale"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hobbies"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"tennis"</span>, <span class="lg-highlight-string">"golf"</span>, <span class="lg-highlight-string">"topiary"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Angelo Ward"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"friends"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Terry Hawkins"</span>, <span class="lg-highlight-string">"Shirley Soto"</span>, <span class="lg-highlight-string">"Joseph Dennis"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hobbies"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"travel"</span>, <span class="lg-highlight-string">"ceramics"</span>, <span class="lg-highlight-string">"golf"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">6</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Shirley Soto"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"friends"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Angelo Ward"</span>, <span class="lg-highlight-string">"Tanya Jordan"</span>, <span class="lg-highlight-string">"Carole Hale"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hobbies"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"frisbee"</span>, <span class="lg-highlight-string">"set theory"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses three stages:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
matches on documents with a
<code class="leafygreen-ui-1l06pbn">
name
</code>
field
containing the string
<code class="leafygreen-ui-1l06pbn">
"Tanya Jordan"
</code>
. Returns one output
document.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-graphLookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</span>
</a>
connects the output document's
<code class="leafygreen-ui-1l06pbn">
friends
</code>
field with the
<code class="leafygreen-ui-1l06pbn">
name
</code>
field of other documents in the
collection to traverse
<code class="leafygreen-ui-1l06pbn">
Tanya Jordan's
</code>
network of connections.
This stage uses the
<code class="leafygreen-ui-1l06pbn">
restrictSearchWithMatch
</code>
parameter to find
only documents in which the
<code class="leafygreen-ui-1l06pbn">
hobbies
</code>
array contains
<code class="leafygreen-ui-1l06pbn">
golf
</code>
.
Returns one output document.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
shapes the output document. The names listed in
<code class="leafygreen-ui-1l06pbn">
connections who play golf
</code>
are taken from the
<code class="leafygreen-ui-1l06pbn">
name
</code>
field of the
documents listed in the input document's
<code class="leafygreen-ui-1l06pbn">
golfers
</code>
array.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.aggregate( [\n { $match: { \"name\": \"Tanya Jordan\" } },\n { $graphLookup: {\n from: \"people\",\n startWith: \"$friends\",\n connectFromField: \"friends\",\n connectToField: \"name\",\n as: \"golfers\",\n restrictSearchWithMatch: { \"hobbies\" : \"golf\" }\n }\n },\n { $project: {\n \"name\": 1,\n \"friends\": 1,\n \"connections who play golf\": \"$golfers.name\"\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"Tanya Jordan"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$graphLookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"people"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startWith</span>: <span class="lg-highlight-string">"$friends"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">connectFromField</span>: <span class="lg-highlight-string">"friends"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">connectToField</span>: <span class="lg-highlight-string">"name"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"golfers"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restrictSearchWithMatch</span>: <!-- -->{ <span class="lg-highlight-string">"hobbies"</span> <!-- -->: <span class="lg-highlight-string">"golf"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"friends"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connections who play golf"</span>: <span class="lg-highlight-string">"$golfers.name"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1,\n \"name\" : \"Tanya Jordan\",\n \"friends\" : [\n \"Shirley Soto\",\n \"Terry Hawkins\",\n \"Carole Hale\"\n ],\n \"connections who play golf\" : [\n \"Joseph Dennis\",\n \"Tanya Jordan\",\n \"Angelo Ward\",\n \"Carole Hale\"\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Tanya Jordan"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"friends"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Shirley Soto"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Terry Hawkins"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Carole Hale"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connections who play golf"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Joseph Dennis"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Tanya Jordan"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Angelo Ward"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Carole Hale"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Additional Resource
<a class="headerlink leafygreen-ui-1kepx47" href="#additional-resource" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="additional-resource">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/presentations/webinar-working-with-graph-data-in-mongodb?tck=docs_server" target="_self">
<span>
Webinar: Working with Graph Data in MongoDB
</span>
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/cursor.hasNext/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
cursor.hasNext()
<a class="headerlink leafygreen-ui-1kepx47" href="#cursor.hasnext--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="cursor.hasnext--">
</div>
</a>
</h1>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
cursor.hasNext()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-cursor.hasNext" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-cursor.hasNext">
</div>
</a>
</dt>
<dd>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for a language-specific driver, such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<table class="css-11h39yh e1nex8bq0">
<colgroup>
<col class="field-name"/>
<col class="field-body"/>
</colgroup>
<tbody>
<tr class="css-x8snli">
<th>
Returns
<!-- -->
:
</th>
<td>
Boolean.
</td>
</tr>
</tbody>
</table>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.hasNext" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.hasNext()
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the cursor returned by
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
query can iterate further to
return more documents.
</p>
</dd>
</dl>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/tanh/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$tanh (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-tanh--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-tanh--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-tanh" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-tanh">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the hyperbolic tangent of a value that is measured in
radians.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $tanh: <expression> }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$tanh</span>: <<!-- -->expression<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
takes any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a number, measured in
radians. If the expression returns a value in degrees, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/degreesToRadians/#mongodb-expression-exp.-degreesToRadians">
<code class="leafygreen-ui-1l06pbn">
$degreesToRadians
</code>
</a>
operator to convert the value to
radians.
</p>
<p class="leafygreen-ui-1kp3ins">
By default
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
returns values as a
<code class="leafygreen-ui-1l06pbn">
double
</code>
.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
can also return values as a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimal
</span>
</a>
if the
<code class="leafygreen-ui-1l06pbn">
<expression>
</code>
resolves to a 128-bit
decimal value.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
</dd>
</dl>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
null
</code>
,
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
, and
<code class="leafygreen-ui-1l06pbn">
+/- Infinity
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#null--nan--and-----infinity" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="null--nan--and-----infinity">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the input argument resolves to a value of
<code class="leafygreen-ui-1l06pbn">
null
</code>
or refers to a
field that is missing,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
. If the
argument resolves to
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
. If
the argument resolves to negative or positive
<code class="leafygreen-ui-1l06pbn">
Infinity
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
-1
</code>
or
<code class="leafygreen-ui-1l06pbn">
1
</code>
respectively.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:50%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Results
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $tanh: NaN }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $tanh: null }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
null
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $tanh: -Infinity }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
-1
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $tanh: Infinity }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
1
</code>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-8">
<div aria-label="Tabs to describe usage of degrees/radians" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_vthq7n9jm" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_8wuy1hjb0" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Hyperbolic Tangent in Degrees
</div>
</button>
<button aria-controls="_ytbwl2sf4" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_1xvj2aw09" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Hyperbolic Tangent in Radians
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_11ljvye1a" class="" id="_shx0ltcxp" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
trigonometry
</code>
collection contains a document
that stores an
<code class="leafygreen-ui-1l06pbn">
angle
</code>
value measured in degrees:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.insertOne(\n {\n \"_id\" : ObjectId( \"5c50782193f833234ba90d45\" ),\n \"angle\" : NumberDecimal( \"53.1301023541559787031443874490659\" )\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">trigonometry</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>( <span class="lg-highlight-string">"5c50782193f833234ba90d45"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>( <span class="lg-highlight-string">"53.1301023541559787031443874490659"</span> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
expression to calculate the hyperbolic
tangent of
<code class="leafygreen-ui-1l06pbn">
angle
</code>
and adds it to the input document using
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
pipeline stage:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.aggregate( [\n {\n $addFields : {\n \"tanh_output\" : { $tanh : { $degreesToRadians : \"$angle\" } }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">trigonometry</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->$addFields<!-- --> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tanh_output"</span> <!-- -->: <!-- -->{ <!-- -->$tanh<!-- --> <!-- -->: <!-- -->{ <!-- -->$degreesToRadians<!-- --> <!-- -->: <span class="lg-highlight-string">"$angle"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/degreesToRadians/#mongodb-expression-exp.-degreesToRadians">
<code class="leafygreen-ui-1l06pbn">
$degreesToRadians
</code>
</a>
expression converts the
<code class="leafygreen-ui-1l06pbn">
angle
</code>
in degrees to radians.
</p>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5c50782193f833234ba90d45\"),\n \"angle\" : NumberDecimal(\"53.1301023541559787031443874490659\"),\n \"tanh_output\" : NumberDecimal(\"0.7293303448445332820512777329448416\")\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5c50782193f833234ba90d45"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"53.1301023541559787031443874490659"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tanh_output"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"0.7293303448445332820512777329448416"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Because
<code class="leafygreen-ui-1l06pbn">
angle
</code>
is stored as a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimal
</span>
</a>
, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
output is also a
128-bit decimal.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_ydiuafdfm" class="leafygreen-ui-38lglc" id="_malrgk7ju" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The following
<code class="leafygreen-ui-1l06pbn">
trigonometry
</code>
collection contains a document
that stores an
<code class="leafygreen-ui-1l06pbn">
angle
</code>
value measured in radians:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.insertOne(\n {\n \"_id\" : ObjectId( \"5c50782193f833234ba90d55\" ),\n \"angle\" : NumberDecimal( \"1.6301023541559787031443874490659\" )\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">trigonometry</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>( <span class="lg-highlight-string">"5c50782193f833234ba90d55"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>( <span class="lg-highlight-string">"1.6301023541559787031443874490659"</span> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
expression to calculate the hyperbolic
tangent of
<code class="leafygreen-ui-1l06pbn">
angle
</code>
and adds it to the input document using
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
pipeline stage:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.trigonometry.aggregate( [\n {\n $addFields : {\n \"tanh_output\" : { $tanh : \"$angle\" }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">trigonometry</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->$addFields<!-- --> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tanh_output"</span> <!-- -->: <!-- -->{ <!-- -->$tanh<!-- --> <!-- -->: <span class="lg-highlight-string">"$angle"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5c50782193f833234ba90d55\"),\n \"angle\" : NumberDecimal(\"1.6301023541559787031443874490659\"),\n \"tanh_output\" : NumberDecimal(\"0.9260761562750713360156803177935379\")\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5c50782193f833234ba90d55"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"angle"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"1.6301023541559787031443874490659"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tanh_output"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"0.9260761562750713360156803177935379"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Because
<code class="leafygreen-ui-1l06pbn">
angle
</code>
is stored as a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-decimal" target="_self">
<span>
128-bit decimal
</span>
</a>
, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-tanh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</span>
</a>
output is also
a 128-bit decimal.
</p>
</div>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/switch/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$switch (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-switch--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-switch--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-switch" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-switch">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Evaluates a series of case expressions. When it finds an expression
which evaluates to
<code class="leafygreen-ui-1l06pbn">
true
</code>
,
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
executes a specified
expression and breaks out of the control flow.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-switch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
</span>
</a>
has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"$switch: {\n branches: [\n { case: <expression>, then: <expression> },\n { case: <expression>, then: <expression> },\n ...\n ],\n default: <expression>\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">$switch</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">branches</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <<!-- -->expression<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">then</span>: <<!-- -->expression<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <<!-- -->expression<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">then</span>: <<!-- -->expression<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">default</span>: <<!-- -->expression<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The objects in the
<code class="leafygreen-ui-1l06pbn">
branches
</code>
array must contain only a
<code class="leafygreen-ui-1l06pbn">
case
</code>
field and a
<code class="leafygreen-ui-1l06pbn">
then
</code>
field.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:65%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Operand
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
branches
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
An array of control branch documents. Each branch is a
document with the following fields:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
<code class="leafygreen-ui-1l06pbn">
case
</code>
</dt>
<dd class="css-1ufxuu7">
Can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a
<code class="leafygreen-ui-1l06pbn">
boolean
</code>
. If the result is not a
<code class="leafygreen-ui-1l06pbn">
boolean
</code>
, it is
coerced to a boolean value. More information about how
MongoDB evaluates expressions as either true or false
can be found
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/and/#std-label-and-boolean-behavior">
here
<!-- -->
.
</a>
</dd>
</dl>
</li>
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
<code class="leafygreen-ui-1l06pbn">
then
</code>
</dt>
<dd class="css-1ufxuu7">
Can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
<!-- -->
.
</a>
</dd>
</dl>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
branches
</code>
array must contain at least one branch
document.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
default
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. The path to take if no branch
<code class="leafygreen-ui-1l06pbn">
case
</code>
expression
evaluates to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Although optional, if
<code class="leafygreen-ui-1l06pbn">
default
</code>
is unspecified and no
branch
<code class="leafygreen-ui-1l06pbn">
case
</code>
evaluates to true,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-switch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
</span>
</a>
returns an error.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The various case statements do not need to be mutually exclusive.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-switch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
</span>
</a>
executes the first branch it finds which
evaluates to
<code class="leafygreen-ui-1l06pbn">
true
</code>
. If none of the branches evaluates to true,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-switch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
</span>
</a>
executes the
<code class="leafygreen-ui-1l06pbn">
default
</code>
option.
</p>
<p class="leafygreen-ui-1kp3ins">
The following conditions cause
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-switch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
</span>
</a>
to fail with an
error:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
branches
</code>
field is missing or is not an array with at least
one entry.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An object in the
<code class="leafygreen-ui-1l06pbn">
branches
</code>
array does not contain a
<code class="leafygreen-ui-1l06pbn">
case
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An object in the
<code class="leafygreen-ui-1l06pbn">
branches
</code>
array does not contain a
<code class="leafygreen-ui-1l06pbn">
then
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An object in the
<code class="leafygreen-ui-1l06pbn">
branches
</code>
array contains a field other than
<code class="leafygreen-ui-1l06pbn">
case
</code>
or
<code class="leafygreen-ui-1l06pbn">
then
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
No
<code class="leafygreen-ui-1l06pbn">
default
</code>
is specified and no
<code class="leafygreen-ui-1l06pbn">
case
</code>
evaluates to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
</li>
</ul>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:70%"/>
<col style="width:30%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Results
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $switch: {\n branches: [\n { case: { $eq: [ 0, 5 ] }, then: \"equals\" },\n { case: { $gt: [ 0, 5 ] }, then: \"greater than\" },\n { case: { $lt: [ 0, 5 ] }, then: \"less than\" }\n ]\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$switch</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">branches</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">5</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"equals"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">5</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"greater than"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <!-- -->{ <span class="lg-highlight-attr">$lt</span>: <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">5</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"less than"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"less than"
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $switch: {\n branches: [\n { case: { $eq: [ 0, 5 ] }, then: \"equals\" },\n { case: { $gt: [ 0, 5 ] }, then: \"greater than\" }\n ],\n default: \"Did not match\"\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$switch</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">branches</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">5</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"equals"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">5</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"greater than"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">default</span>: <span class="lg-highlight-string">"Did not match"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"Did not match"
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $switch: {\n branches: [\n { case: \"this is true\", then: \"first case\" },\n { case: false, then: \"second case\" }\n ],\n default: \"Did not match\"\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$switch</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">branches</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <span class="lg-highlight-string">"this is true"</span>, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"first case"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <span class="lg-highlight-literal">false</span>, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"second case"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">default</span>: <span class="lg-highlight-string">"Did not match"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"First case"
</code>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
A collection named
<code class="leafygreen-ui-1l06pbn">
grades
</code>
contains the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"name\" : \"Susan Wilkes\", \"scores\" : [ 87, 86, 78 ] }\n{ \"_id\" : 2, \"name\" : \"Bob Hanna\", \"scores\" : [ 71, 64, 81 ] }\n{ \"_id\" : 3, \"name\" : \"James Torrelio\", \"scores\" : [ 91, 84, 97 ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Susan Wilkes"</span>, <span class="lg-highlight-string">"scores"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">87</span>, <span class="lg-highlight-number">86</span>, <span class="lg-highlight-number">78</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Bob Hanna"</span>, <span class="lg-highlight-string">"scores"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">71</span>, <span class="lg-highlight-number">64</span>, <span class="lg-highlight-number">81</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"James Torrelio"</span>, <span class="lg-highlight-string">"scores"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">91</span>, <span class="lg-highlight-number">84</span>, <span class="lg-highlight-number">97</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation uses
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-switch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
</span>
</a>
to
display a particular message based on each student's average score.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.grades.aggregate( [\n {\n $project:\n {\n \"name\" : 1,\n \"summary\" :\n {\n $switch:\n {\n branches: [\n {\n case: { $gte : [ { $avg : \"$scores\" }, 90 ] },\n then: \"Doing great!\"\n },\n {\n case: { $and : [ { $gte : [ { $avg : \"$scores\" }, 80 ] },\n { $lt : [ { $avg : \"$scores\" }, 90 ] } ] },\n then: \"Doing pretty well.\"\n },\n {\n case: { $lt : [ { $avg : \"$scores\" }, 80 ] },\n then: \"Needs improvement.\"\n }\n ],\n default: \"No scores found.\"\n }\n }\n }\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">grades</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$project</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"summary"</span> <!-- -->:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$switch</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">branches</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">case</span>: <!-- -->{ <!-- -->$gte<!-- --> <!-- -->: <!-- -->[ <!-- -->{ <!-- -->$avg<!-- --> <!-- -->: <span class="lg-highlight-string">"$scores"</span> }<!-- -->, <span class="lg-highlight-number">90</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"Doing great!"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">case</span>: <!-- -->{ <!-- -->$and<!-- --> <!-- -->: <!-- -->[ <!-- -->{ <!-- -->$gte<!-- --> <!-- -->: <!-- -->[ <!-- -->{ <!-- -->$avg<!-- --> <!-- -->: <span class="lg-highlight-string">"$scores"</span> }<!-- -->, <span class="lg-highlight-number">80</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$lt<!-- --> <!-- -->: <!-- -->[ <!-- -->{ <!-- -->$avg<!-- --> <!-- -->: <span class="lg-highlight-string">"$scores"</span> }<!-- -->, <span class="lg-highlight-number">90</span> ] } ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"Doing pretty well."</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">case</span>: <!-- -->{ <!-- -->$lt<!-- --> <!-- -->: <!-- -->[ <!-- -->{ <!-- -->$avg<!-- --> <!-- -->: <span class="lg-highlight-string">"$scores"</span> }<!-- -->, <span class="lg-highlight-number">80</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"Needs improvement."</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">default</span>: <span class="lg-highlight-string">"No scores found."</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"name\" : \"Susan Wilkes\", \"summary\" : \"Doing pretty well.\" }\n{ \"_id\" : 2, \"name\" : \"Bob Hanna\", \"summary\" : \"Needs improvement.\" }\n{ \"_id\" : 3, \"name\" : \"James Torrelio\", \"summary\" : \"Doing great!\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Susan Wilkes"</span>, <span class="lg-highlight-string">"summary"</span> <!-- -->: <span class="lg-highlight-string">"Doing pretty well."</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Bob Hanna"</span>, <span class="lg-highlight-string">"summary"</span> <!-- -->: <span class="lg-highlight-string">"Needs improvement."</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"James Torrelio"</span>, <span class="lg-highlight-string">"summary"</span> <!-- -->: <span class="lg-highlight-string">"Doing great!"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/cond/#mongodb-expression-exp.-cond">
<code class="leafygreen-ui-1l06pbn">
$cond
</code>
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$elemMatch (projection)
<a class="headerlink leafygreen-ui-1kepx47" href="#-elemmatch--projection-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-elemmatch--projection-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#usage-considerations" target="_self">
<span>
Usage Considerations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/elemMatch/">
$elemMatch (query)
</a>
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="projection">
<dt>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-projection-proj.-elemMatch" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-projection-proj.-elemMatch">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
operator limits the contents of an
<code class="leafygreen-ui-1l06pbn">
<array>
</code>
field from the query results to contain only the
<strong>
first
</strong>
element matching the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
condition.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Usage Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#usage-considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="usage-considerations">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Returned Element
<a class="headerlink leafygreen-ui-1kepx47" href="#returned-element" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="returned-element">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Both the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/positional/#mongodb-projection-proj.-">
<code class="leafygreen-ui-1l06pbn">
$
</code>
</a>
operator and the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
operator project
the
<strong>
first
</strong>
matching element from an array based on a condition.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/positional/#mongodb-projection-proj.-">
<code class="leafygreen-ui-1l06pbn">
$
</code>
</a>
operator projects the first matching array element from each
document in a collection based on some condition from the query statement.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
projection operator takes an explicit condition
argument. This allows you to project based on a condition not in the query, or
if you need to project based on multiple fields in the array's embedded documents.
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/positional/#std-label-array-field-limitation">
Array Field Limitations
</a>
for an example.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Field Order
<a class="headerlink leafygreen-ui-1kepx47" href="#field-order" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="field-order">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Regardless of the ordering of the fields in the document, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
projection of an existing field returns
the field after the other existing field inclusions.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider a
<code class="leafygreen-ui-1l06pbn">
players
</code>
collection with the following document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.players.insertOne( {\n name: \"player1\",\n games: [ { game: \"abc\", score: 8 }, { game: \"xyz\", score: 5 } ],\n joined: new Date(\"2020-01-01\"),\n lastLogin: new Date(\"2020-05-01\")\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">players</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"player1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">games</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">game</span>: <span class="lg-highlight-string">"abc"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">8</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">game</span>: <span class="lg-highlight-string">"xyz"</span>, <span class="lg-highlight-attr">score</span>: <span class="lg-highlight-number">5</span> } ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">joined</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2020-01-01"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">lastLogin</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2020-05-01"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following projection returns the
<code class="leafygreen-ui-1l06pbn">
games
</code>
field
after the other existing fields included in the projection even though
in the document, the field is listed before
<code class="leafygreen-ui-1l06pbn">
joined
</code>
and
<code class="leafygreen-ui-1l06pbn">
lastLogin
</code>
fields:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.players.find( {}, { games: { $elemMatch: { score: { $gt: 5 } } }, joined: 1, lastLogin: 1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">players</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{<!-- -->}<!-- -->, <!-- -->{ <span class="lg-highlight-attr">games</span>: <!-- -->{ <span class="lg-highlight-attr">$elemMatch</span>: <!-- -->{ <span class="lg-highlight-attr">score</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">5</span> } } }<!-- -->, <span class="lg-highlight-attr">joined</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">lastLogin</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
That is, the operation returns the following document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5edef64a1c099fff6b033977\"),\n \"joined\" : ISODate(\"2020-01-01T00:00:00Z\"),\n \"lastLogin\" : ISODate(\"2020-05-01T00:00:00Z\"),\n \"games\" : [ { \"game\" : \"abc\", \"score\" : 8 } ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5edef64a1c099fff6b033977"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"joined"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-01T00:00:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"lastLogin"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-01T00:00:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"games"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"game"</span> <!-- -->: <span class="lg-highlight-string">"abc"</span>, <span class="lg-highlight-string">"score"</span> <!-- -->: <span class="lg-highlight-number">8</span> } ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Restrictions
<a class="headerlink leafygreen-ui-1kepx47" href="#restrictions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="restrictions">
</div>
</a>
</h3>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
operations on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
views
</a>
do not support
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
projection operator.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You cannot specify a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/text/#mongodb-query-op.-text">
<code class="leafygreen-ui-1l06pbn">
$text
</code>
</a>
query operator in an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
.
</span>
</a>
</p>
</li>
</ul>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The examples on the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
projection operator
assumes a collection
<code class="leafygreen-ui-1l06pbn">
schools
</code>
with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 1,\n zipcode: \"63109\",\n students: [\n { name: \"john\", school: 102, age: 10 },\n { name: \"jess\", school: 102, age: 11 },\n { name: \"jeff\", school: 108, age: 15 }\n ]\n}\n{\n _id: 2,\n zipcode: \"63110\",\n students: [\n { name: \"ajax\", school: 100, age: 7 },\n { name: \"achilles\", school: 100, age: 8 },\n ],\n athletics: [ \"swimming\", \"basketball\", \"football\" ]\n}\n{\n _id: 3,\n zipcode: \"63109\",\n students: [\n { name: \"ajax\", school: 100, age: 7 },\n { name: \"achilles\", school: 100, age: 8 },\n ],\n athletics: [ \"baseball\", \"basketball\", \"soccer\" ]\n}\n{\n _id: 4,\n zipcode: \"63109\",\n students: [\n { name: \"barney\", school: 102, age: 7 },\n { name: \"ruth\", school: 102, age: 16 },\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">zipcode</span>: <span class="lg-highlight-string">"63109"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">students</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"john"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">10</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"jess"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">11</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"jeff"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">108</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">15</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">zipcode</span>: <span class="lg-highlight-string">"63110"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">students</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"ajax"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">7</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"achilles"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">8</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">athletics</span>: <!-- -->[ <span class="lg-highlight-string">"swimming"</span>, <span class="lg-highlight-string">"basketball"</span>, <span class="lg-highlight-string">"football"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">zipcode</span>: <span class="lg-highlight-string">"63109"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">students</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"ajax"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">7</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"achilles"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">8</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">athletics</span>: <!-- -->[ <span class="lg-highlight-string">"baseball"</span>, <span class="lg-highlight-string">"basketball"</span>, <span class="lg-highlight-string">"soccer"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">zipcode</span>: <span class="lg-highlight-string">"63109"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">students</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"barney"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">7</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"ruth"</span>, <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">16</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Zipcode Search
<a class="headerlink leafygreen-ui-1kepx47" href="#zipcode-search" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="zipcode-search">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</a>
operation
queries for all documents where the value of the
<code class="leafygreen-ui-1l06pbn">
zipcode
</code>
field is
<code class="leafygreen-ui-1l06pbn">
63109
</code>
. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
projection
returns only the
<strong>
first
</strong>
matching element of the
<code class="leafygreen-ui-1l06pbn">
students
</code>
array where the
<code class="leafygreen-ui-1l06pbn">
school
</code>
field has a value of
<code class="leafygreen-ui-1l06pbn">
102
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.schools.find( { zipcode: \"63109\" },\n { students: { $elemMatch: { school: 102 } } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">schools</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">zipcode</span>: <span class="lg-highlight-string">"63109"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">students</span>: <!-- -->{ <span class="lg-highlight-attr">$elemMatch</span>: <!-- -->{ <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">102</span> } } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following documents that have
<code class="leafygreen-ui-1l06pbn">
zipcode
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
63109
</code>
and projects the
<code class="leafygreen-ui-1l06pbn">
students
</code>
array using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"students\" : [ { \"name\" : \"john\", \"school\" : 102, \"age\" : 10 } ] }\n{ \"_id\" : 3 }\n{ \"_id\" : 4, \"students\" : [ { \"name\" : \"barney\", \"school\" : 102, \"age\" : 7 } ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"students"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"john"</span>, <span class="lg-highlight-string">"school"</span> <!-- -->: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-string">"age"</span> <!-- -->: <span class="lg-highlight-number">10</span> } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"students"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"barney"</span>, <span class="lg-highlight-string">"school"</span> <!-- -->: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-string">"age"</span> <!-- -->: <span class="lg-highlight-number">7</span> } ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For the document with
<code class="leafygreen-ui-1l06pbn">
_id
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
1
</code>
, the
<code class="leafygreen-ui-1l06pbn">
students
</code>
array contains multiple elements with the
<code class="leafygreen-ui-1l06pbn">
school
</code>
field
equal to
<code class="leafygreen-ui-1l06pbn">
102
</code>
. However, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
projection returns only the first matching element from the
array.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The document with
<code class="leafygreen-ui-1l06pbn">
_id
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
3
</code>
does not contain the
<code class="leafygreen-ui-1l06pbn">
students
</code>
field in the result since no element in its
<code class="leafygreen-ui-1l06pbn">
students
</code>
array matched the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
condition.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
with Multiple Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#with-multiple-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="with-multiple-fields">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
projection can specify criteria on multiple
fields:
</p>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</a>
operation
queries for all documents where the value of the
<code class="leafygreen-ui-1l06pbn">
zipcode
</code>
field is
<code class="leafygreen-ui-1l06pbn">
63109
</code>
. The projection includes the
<strong>
first
</strong>
matching element of the
<code class="leafygreen-ui-1l06pbn">
students
</code>
array where the
<code class="leafygreen-ui-1l06pbn">
school
</code>
field has a value of
<code class="leafygreen-ui-1l06pbn">
102
</code>
<strong>
and
</strong>
the
<code class="leafygreen-ui-1l06pbn">
age
</code>
field is greater
than
<code class="leafygreen-ui-1l06pbn">
10
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.schools.find( { zipcode: \"63109\" },\n { students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">schools</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">zipcode</span>: <span class="lg-highlight-string">"63109"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">students</span>: <!-- -->{ <span class="lg-highlight-attr">$elemMatch</span>: <!-- -->{ <span class="lg-highlight-attr">school</span>: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-attr">age</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">10</span>} } } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the three documents that have
<code class="leafygreen-ui-1l06pbn">
zipcode
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
63109
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"students\" : [ { \"name\" : \"jess\", \"school\" : 102, \"age\" : 11 } ] }\n{ \"_id\" : 3 }\n{ \"_id\" : 4, \"students\" : [ { \"name\" : \"ruth\", \"school\" : 102, \"age\" : 16 } ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"students"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"jess"</span>, <span class="lg-highlight-string">"school"</span> <!-- -->: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-string">"age"</span> <!-- -->: <span class="lg-highlight-number">11</span> } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"students"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"ruth"</span>, <span class="lg-highlight-string">"school"</span> <!-- -->: <span class="lg-highlight-number">102</span>, <span class="lg-highlight-string">"age"</span> <!-- -->: <span class="lg-highlight-number">16</span> } ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The document with
<code class="leafygreen-ui-1l06pbn">
_id
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
3
</code>
does not contain the
<code class="leafygreen-ui-1l06pbn">
students
</code>
field
since no array element matched the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
criteria.
</p>
<p class="leafygreen-ui-1kp3ins">
The argument to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
matches elements of the array that
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
is projecting. If you specify an equality with a field
name to
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
, it attempts to match objects within the array.
For example,
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
attempts to match objects, instead of scalar
values, within the array for the following in the projection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.schools.find( { zipcode: \"63109\" },\n { athletics: { $elemMatch: { athletics: \"basketball\" } } })","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">schools</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">zipcode</span>: <span class="lg-highlight-string">"63109"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">athletics</span>: <!-- -->{ <span class="lg-highlight-attr">$elemMatch</span>: <!-- -->{ <span class="lg-highlight-attr">athletics</span>: <span class="lg-highlight-string">"basketball"</span> } } })</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To match scalar values, use the equality operator along with the scalar
value that you want to match (
<code class="leafygreen-ui-1l06pbn">
{$eq: <scalar value>}
</code>
). For example,
the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</a>
operation queries for all
documents where the value of the
<code class="leafygreen-ui-1l06pbn">
zipcode
</code>
field is
<code class="leafygreen-ui-1l06pbn">
63109
</code>
. The
projection includes the matching element of the
<code class="leafygreen-ui-1l06pbn">
athletics
</code>
array
where the value is
<code class="leafygreen-ui-1l06pbn">
basketball
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.schools.find( { zipcode: \"63109\" },\n { athletics: { $elemMatch: { $eq: \"basketball\" } } })","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">schools</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">zipcode</span>: <span class="lg-highlight-string">"63109"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">athletics</span>: <!-- -->{ <span class="lg-highlight-attr">$elemMatch</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <span class="lg-highlight-string">"basketball"</span> } } })</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the three documents that have
<code class="leafygreen-ui-1l06pbn">
zipcode
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
63109
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n { _id : 1 },\n { _id: 3, athletics: [ 'basketball' ] },\n { _id : 4 }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">athletics</span>: <!-- -->[ <span class="lg-highlight-string">'basketball'</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The document with
<code class="leafygreen-ui-1l06pbn">
_id
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
3
</code>
is the only document that
matched the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-projection-proj.-elemMatch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</span>
</a>
criteria.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/positional/#mongodb-projection-proj.-">
<code class="leafygreen-ui-1l06pbn">
$ (projection)
</code>
</a>
operator
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/defragment-sharded-collections/start-defragmenting-sharded-collection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Start Defragmenting a Sharded Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#start-defragmenting-a-sharded-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-defragmenting-a-sharded-collection">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#about-this-task" target="_self">
<span>
About this Task
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#before-you-begin" target="_self">
<span>
Before you Begin
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#next-steps" target="_self">
<span>
Next Steps
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
To start defragmenting a sharded collection, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/configureCollectionBalancing/#mongodb-dbcommand-dbcmd.configureCollectionBalancing">
<code class="leafygreen-ui-1l06pbn">
configureCollectionBalancing
</code>
</a>
command with the
<code class="leafygreen-ui-1l06pbn">
defragmentCollection
</code>
option set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
About this Task
<a class="headerlink leafygreen-ui-1kepx47" href="#about-this-task" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="about-this-task">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Fragmentation is where a sharded collection's data is broken up into an
unnecessarily large number of small chunks. This can increase operation
times of CRUD operations run on that collection. Defragmentation reduces
the number of chunks by merging smaller chunks into larger ones,
resulting in lower CRUD operation times.
</p>
<p class="leafygreen-ui-1kp3ins">
If CRUD operation times are acceptable, you don't need to defragment
collections.
</p>
<p class="leafygreen-ui-1kp3ins">
The following table summarizes defragmentation information for various
MongoDB versions.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:70%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Version
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB 7.0 and later
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Chunks are automatically merged. Performance improvements from
defragmenting a collection in MongoDB 7.0 are lower compared to
MongoDB 6.0. Typically, you don't need to defragment collections
starting in MongoDB 7.0.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB 6.0 and earlier than 7.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Defragment collections only if you experience CRUD operation
delays when the balancer migrates chunks or a node starts.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, high write traffic should not cause
fragmentation. Chunk migrations cause fragmentation.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Earlier than MongoDB 6.0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Defragment collections only if you experience longer CRUD
operation times during metadata updates. For MongoDB versions
earlier than 6.0, a sharded collection becomes fragmented when
the collection size grows significantly because of many insert or
update operations.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The procedure in this task uses an example sharded collection named
<code class="leafygreen-ui-1l06pbn">
ordersShardedCollection
</code>
in a database named
<code class="leafygreen-ui-1l06pbn">
test
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
You can use your own sharded collection and database in the procedure.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Before you Begin
<a class="headerlink leafygreen-ui-1kepx47" href="#before-you-begin" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="before-you-begin">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Connect to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Run:","name":"Start defragmenting the collection"},{"@type":"HowToStep","text":"Ensure ok is 1 in the command output, which indicates the\ncommand execution was successful:","name":"Ensure defragmentation started"}],"name":"Procedure","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start defragmenting the collection
<a class="headerlink leafygreen-ui-1kepx47" href="#start-defragmenting-the-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-defragmenting-the-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand(\n {\n configureCollectionBalancing: \"test.ordersShardedCollection\",\n defragmentCollection: true\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">configureCollectionBalancing</span>: <span class="lg-highlight-string">"test.ordersShardedCollection"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">defragmentCollection</span>: <span class="lg-highlight-literal">true</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Ensure defragmentation started
<a class="headerlink leafygreen-ui-1kepx47" href="#ensure-defragmentation-started" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="ensure-defragmentation-started">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Ensure
<code class="leafygreen-ui-1l06pbn">
ok
</code>
is
<code class="leafygreen-ui-1l06pbn">
1
</code>
in the command output, which indicates the
command execution was successful:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n ok: 1,\n '$clusterTime': {\n clusterTime: Timestamp({ t: 1677616966, i: 8 }),\n signature: {\n hash: Binary(Buffer.from(\"0000000000000000000000000000000000000000\", \"hex\"), 0),\n keyId: Long(\"0\")\n }\n },\n operationTime: Timestamp({ t: 1677616966, i: 8 })\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ok</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'$clusterTime'</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clusterTime</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<!-- -->{ <span class="lg-highlight-attr">t</span>: <span class="lg-highlight-number">1677616966</span>, <span class="lg-highlight-attr">i</span>: <span class="lg-highlight-number">8</span> })<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">signature</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hash</span>: <span class="lg-highlight-title lg-highlight-class">Binary</span>(<span class="lg-highlight-title lg-highlight-class">Buffer</span>.<span class="lg-highlight-title lg-highlight-function">from</span>(<span class="lg-highlight-string">"0000000000000000000000000000000000000000"</span>, <span class="lg-highlight-string">"hex"</span>)<!-- -->, <span class="lg-highlight-number">0</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyId</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"0"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">operationTime</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>(<!-- -->{ <span class="lg-highlight-attr">t</span>: <span class="lg-highlight-number">1677616966</span>, <span class="lg-highlight-attr">i</span>: <span class="lg-highlight-number">8</span> })</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Next Steps
<a class="headerlink leafygreen-ui-1kepx47" href="#next-steps" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="next-steps">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can monitor the collection's defragmentation progress. For details,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/defragment-sharded-collections/monitor-defragmentation-sharded-collection/#std-label-monitor-defragmentation-sharded-collection">
Monitor Defragmentation of a Sharded Collection
<!-- -->
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Learn More
<a class="headerlink leafygreen-ui-1kepx47" href="#learn-more" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="learn-more">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Print shard status, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.printShardingStatus/#mongodb-method-db.printShardingStatus">
<code class="leafygreen-ui-1l06pbn">
db.printShardingStatus()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Retrieve shard status details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-method-sh.status">
<code class="leafygreen-ui-1l06pbn">
sh.status()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
View shard status collection fields, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#std-label-sharding-status-collection-fields">
Sharded Collection
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
See active mongos instances, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#std-label-sharding-status-mongoses">
Active
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
Instances
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Monitor shards using MongoDB Atlas, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/review-sharded-cluster-metrics/#review-sharded-clusters/" target="_self">
<span>
Review Sharded Clusters
</span>
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-enterprise-on-windows/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Install MongoDB Enterprise Edition on Windows
<a class="headerlink leafygreen-ui-1kepx47" href="#install-mongodb-enterprise-edition-on-windows" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="install-mongodb-enterprise-edition-on-windows">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#overview" target="_self">
<span>
Overview
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#considerations" target="_self">
<span>
Considerations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#install-mongodb-enterprise-edition" target="_self">
<span>
Install MongoDB Enterprise Edition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#start-mongodb-enterprise-edition-from-the-command-interpreter" target="_self">
<span>
Start MongoDB Enterprise Edition from the Command Interpreter
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#start-mongodb-enterprise-edition-as-a-windows-service" target="_self">
<span>
Start MongoDB Enterprise Edition as a Windows Service
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#stop-mongodb-enterprise-edition-as-a-windows-service" target="_self">
<span>
Stop MongoDB Enterprise Edition as a Windows Service
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#remove-mongodb-enterprise-edition-as-a-windows-service" target="_self">
<span>
Remove MongoDB Enterprise Edition as a Windows Service
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#additional-considerations" target="_self">
<span>
Additional Considerations
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Overview
<a class="headerlink leafygreen-ui-1kepx47" href="#overview" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="overview">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use this tutorial to install MongoDB 8.0
<!-- -->
Enterprise
<!-- -->
Edition on
Windows using the default installation wizard.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/mongodb-enterprise-advanced?tck=docs_server" target="_self">
<span>
MongoDB Enterprise Edition
</span>
</a>
is available on select platforms and contains support for several
features related to security and monitoring.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
MongoDB Version
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-version" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-version">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This tutorial installs MongoDB 8.0
<!-- -->
Enterprise
<!-- -->
Edition. To install a different version of MongoDB
<!-- -->
Enterprise
<!-- -->
,
use the version drop-down menu in the upper-left corner of this page to
select the documentation for that version.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Installation Method
<a class="headerlink leafygreen-ui-1kepx47" href="#installation-method" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="installation-method">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This tutorial installs MongoDB on Windows using the default MSI
installation wizard. To install MongoDB using the
<code class="leafygreen-ui-1l06pbn">
msiexec.exe
</code>
command-line tool instead, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-windows-unattended/#std-label-install-enterprise-windows-msiexec">
Install MongoDB using msiexec.exe
</a>
. The
<code class="leafygreen-ui-1l06pbn">
msiexec.exe
</code>
tool is useful for system administrators who wish to
deploy MongoDB in an unattended fashion using automation.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="considerations">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
MongoDB Shell,
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-shell--mongosh" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-shell--mongosh">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The MongoDB Shell (
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
) is not installed with
MongoDB Server. You need to follow the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/install/" target="_self">
<span>
mongosh
installation instructions
</span>
</a>
to download and install
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
separately.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Platform Support
<a class="headerlink leafygreen-ui-1kepx47" href="#platform-support" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="platform-support">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 8.0 Enterprise Edition supports the following
<strong class="css-4dei7l">
64-bit
</strong>
versions of Windows on x86_64 architecture:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Windows Server 2022
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Windows Server 2019
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Windows 11
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
MongoDB only supports the 64-bit versions of these platforms.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/production-notes/#std-label-prod-notes-supported-platforms">
Platform Support
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
MongoDB is not supported on Windows Subsystem for Linux (WSL). To
run MongoDB on Linux, use a supported Linux system.
</p>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Virtualization
<a class="headerlink leafygreen-ui-1kepx47" href="#virtualization" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="virtualization">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Oracle offers
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://docs.oracle.com/en/virtualization/virtualbox/6.0/admin/hyperv-support.html" rel="noopener noreferrer">
<span>
experimental support
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
for VirtualBox on Windows hosts where Hyper-V is running. However,
Microsoft does not support
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://docs.microsoft.com/en-us/troubleshoot/windows-client/application-management/virtualization-apps-not-work-with-hyper-v" rel="noopener noreferrer">
<span>
VirtualBox on Hyper-V
<!-- -->
.
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Disable Hyper-V if you want to install MongoDB on Windows using VirtualBox.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Production Notes
<a class="headerlink leafygreen-ui-1kepx47" href="#production-notes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="production-notes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Before deploying MongoDB in a production environment, consider the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/production-notes/">
Production Notes for Self-Managed Deployments
</a>
document which offers
performance considerations and configuration recommendations for
production MongoDB deployments.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Full Time Diagnostic Data Capture
<a class="headerlink leafygreen-ui-1kepx47" href="#full-time-diagnostic-data-capture" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="full-time-diagnostic-data-capture">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB logs diagnostic data to assist with troubleshooting. For
detailed information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/full-time-diagnostic-data-capture/#std-label-ftdc-stub">
Full Time Diagnostic Data Capture
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
On Windows, to collect system data such as disk, cpu, and memory, FTDC requires Microsoft access permissions from the following groups:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Performance Monitor Users
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Performance Log Users
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If the user running
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
is not an administrator, add them to these groups to log
FTDC data. For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://learn.microsoft.com/en-us/windows/win32/perfctrs/restricting-access-to-performance-extension--dlls" rel="noopener noreferrer">
<span>
the Microsoft documentation here
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Install MongoDB Enterprise Edition
<a class="headerlink leafygreen-ui-1kepx47" href="#install-mongodb-enterprise-edition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="install-mongodb-enterprise-edition">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Follow these steps to install MongoDB
<!-- -->
Enterprise
<!-- -->
Edition using the
Windows Installation wizard. The installation process installs both the
MongoDB binaries as well as the default
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#std-label-configuration-options">
configuration file
</a>
<code class="file docutils literal">
<span class="pre">
<install
directory>\bin\mongod.cfg
</span>
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Download the installer.Download the MongoDB Enterprise .msi installer from the following\nlink:➤ MongoDB Download CenterIn the Version dropdown, select the version of\nMongoDB to download.In the Platform dropdown, select Windows.In the Package dropdown, select msi.Click Download."},{"@type":"HowToStep","text":"Run the MongoDB installer.For example, from the Windows Explorer/File Explorer:Go to the directory where you downloaded the MongoDB installer (.msi file).\nBy default, this is your Downloads directory.Double-click the .msi file."},{"@type":"HowToStep","text":"Follow the MongoDB Enterprise Edition installation wizard.The wizard steps you through the installation of MongoDB and MongoDB\nCompass.You can choose either the Complete (recommended for\nmost users) or Custom setup type. The\nComplete setup option installs MongoDB and the\nMongoDB tools to the default location. The Custom\nsetup option allows you to specify which executables are\ninstalled and where.You can set up MongoDB as a Windows service during the install or just\ninstall the binaries.You can configure and start MongoDB as a Windows service\nduring the install, and the MongoDB service is started upon\nsuccessful installation.Select Install MongoD as a Service.Select one of these options:Run the service as Network Service user (Default)This is a Windows user account that is built-in\nto Windows.Run the service as a local or domain userFor an existing local user account, specify a\nperiod (.) for the Account\nDomain and specify the Account Name and\nthe Account Password for the user.For an existing domain user, specify the\nAccount Domain, Account\nName and Account Password for that\nuser.Service Name. Specify the service\nname. Default name is MongoDB. If you already\nhave a service with the specified name, you must\nchoose another name.Data Directory. Specify the data\ndirectory, which corresponds to the\n--dbpath. If the\ndirectory does not exist, the installer will\ncreate the directory and sets the directory access\nto the service user.Log Directory. Specify the Log\ndirectory, which corresponds to the\n--logpath. If the\ndirectory does not exist, the installer will\ncreate the directory and sets the directory access\nto the service user.If you choose not to configure MongoDB as a Windows service,\nuncheck the Install MongoD as a Service.Optional. To have the wizard install MongoDB Compass, select\nInstall MongoDB Compass (Default).When ready, click Install."}],"name":"Procedure","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Download the installer.
<a class="headerlink leafygreen-ui-1kepx47" href="#download-the-installer" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="download-the-installer">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Download the MongoDB
<!-- -->
Enterprise
<!-- -->
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
installer from the following
link:
</p>
<p class="leafygreen-ui-1kp3ins">
➤
<!-- -->
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/try/download/enterprise?tck=docs_server" target="_self">
<span>
MongoDB Download Center
</span>
</a>
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In the
<span class="guilabel css-h15tq0">
Version
</span>
dropdown, select the version of
MongoDB to download.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In the
<span class="guilabel css-h15tq0">
Platform
</span>
dropdown, select
<span class="guilabel css-h15tq0">
Windows
</span>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In the
<span class="guilabel css-h15tq0">
Package
</span>
dropdown, select
<span class="guilabel css-h15tq0">
msi
</span>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Click
<span class="guilabel css-h15tq0">
Download
</span>
.
</p>
</li>
</ol>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Run the MongoDB installer.
<a class="headerlink leafygreen-ui-1kepx47" href="#run-the-mongodb-installer" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="run-the-mongodb-installer">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
For example, from the Windows Explorer/File Explorer:
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Go to the directory where you downloaded the MongoDB installer (
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
file).
By default, this is your
<code class="leafygreen-ui-1l06pbn">
Downloads
</code>
directory.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Double-click the
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
file.
</p>
</li>
</ol>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Follow the MongoDB
<!-- -->
Enterprise
<!-- -->
Edition installation wizard.
<a class="headerlink leafygreen-ui-1kepx47" href="#follow-the-mongodb-edition-installation-wizard" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="follow-the-mongodb-edition-installation-wizard">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The wizard steps you through the installation of MongoDB and MongoDB
Compass.
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
<span class="guilabel css-h15tq0">
Choose Setup Type
</span>
</dt>
<dd class="css-1ufxuu7">
You can choose either the
<span class="guilabel css-h15tq0">
Complete
</span>
(recommended for
most users) or
<span class="guilabel css-h15tq0">
Custom
</span>
setup type. The
<span class="guilabel css-h15tq0">
Complete
</span>
setup option installs MongoDB and the
MongoDB tools to the default location. The
<span class="guilabel css-h15tq0">
Custom
</span>
setup option allows you to specify which executables are
installed and where.
</dd>
</dl>
</li>
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
<span class="guilabel css-h15tq0">
Service Configuration
</span>
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
You can set up MongoDB as a Windows service during the install or just
install the binaries.
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-18">
<div aria-label="Tabs to describe usage of mongodb/service" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_zval65sl0" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_l4bx7947e" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
MongoDB Service
</div>
</button>
<button aria-controls="_m0fndhdlu" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_d42rfta91" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
MongoDB
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_vxjqfbx74" class="" id="_6cm0y4wnj" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can configure and start MongoDB as a Windows service
during the install, and the MongoDB service is started upon
successful installation.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Select
<span class="guilabel css-h15tq0">
Install MongoD as a Service
</span>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Select one of these options:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<span class="guilabel css-h15tq0">
Run the service as Network Service user
</span>
(Default)
</p>
<p class="leafygreen-ui-1kp3ins">
This is a Windows user account that is built-in
to Windows.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<span class="guilabel css-h15tq0">
Run the service as a local or domain user
</span>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For an existing local user account, specify a
period (
<code class="leafygreen-ui-1l06pbn">
.
</code>
) for the
<span class="guilabel css-h15tq0">
Account
Domain
</span>
and specify the
<span class="guilabel css-h15tq0">
Account Name
</span>
and
the
<span class="guilabel css-h15tq0">
Account Password
</span>
for the user.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For an existing domain user, specify the
<span class="guilabel css-h15tq0">
Account Domain
</span>
,
<span class="guilabel css-h15tq0">
Account
Name
</span>
and
<span class="guilabel css-h15tq0">
Account Password
</span>
for that
user.
</p>
</li>
</ul>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<span class="guilabel css-h15tq0">
Service Name
</span>
. Specify the service
name. Default name is
<code class="leafygreen-ui-1l06pbn">
MongoDB
</code>
. If you already
have a service with the specified name, you must
choose another name.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<span class="guilabel css-h15tq0">
Data Directory
</span>
. Specify the data
directory, which corresponds to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
. If the
directory does not exist, the installer will
create the directory and sets the directory access
to the service user.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<span class="guilabel css-h15tq0">
Log Directory
</span>
. Specify the Log
directory, which corresponds to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--logpath">
<code class="leafygreen-ui-1l06pbn">
--logpath
</code>
</a>
. If the
directory does not exist, the installer will
create the directory and sets the directory access
to the service user.
</p>
</li>
</ul>
</div>
</div>
</div>
<div>
<div aria-labelledby="_51dbo7es3" class="leafygreen-ui-38lglc" id="_zco87lqc4" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If you choose not to configure MongoDB as a Windows service,
uncheck the
<span class="guilabel css-h15tq0">
Install MongoD as a Service
</span>
.
</p>
</div>
</div>
</div>
</div>
</div>
</dd>
</dl>
</li>
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
<span class="guilabel css-h15tq0">
Install MongoDB Compass
</span>
</dt>
<dd class="css-1ufxuu7">
<em>
Optional
</em>
. To have the wizard install
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https:///www.mongodb.com/products/compass" target="_self">
<span>
MongoDB Compass
</span>
</a>
, select
<span class="guilabel css-h15tq0">
Install MongoDB Compass
</span>
(Default).
</dd>
</dl>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When ready, click
<span class="guilabel css-h15tq0">
Install
</span>
.
</p>
</li>
</ol>
</section>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
If You Installed MongoDB as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#if-you-installed-mongodb-as-a-windows-service" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="if-you-installed-mongodb-as-a-windows-service">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The MongoDB service starts upon successful installation. Configure the
MongoDB instance with the configuration file
<code class="file docutils literal">
<span class="pre">
<install directory>\bin\mongod.cfg
</span>
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If you have not already done so, follow the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/install/" target="_self">
<span>
mongosh installation instructions
</span>
</a>
to download and
install the MongoDB Shell (
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
).
</p>
<p class="leafygreen-ui-1kp3ins">
Be sure to add the path to your
<code class="leafygreen-ui-1l06pbn">
mongosh.exe
</code>
binary to your
<code class="leafygreen-ui-1l06pbn">
PATH
</code>
environment variable during installation.
</p>
<p class="leafygreen-ui-1kp3ins">
Open a new
<span class="guilabel css-h15tq0">
Command Interpreter
</span>
and enter
<code class="leafygreen-ui-1l06pbn">
mongosh.exe
</code>
to connect to MongoDB.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on connecting to
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh.exe
</span>
</a>
, such as connecting to a MongoDB instance
running on a different host and/or port, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/connect/" target="_self">
<span>
Connect to a Deployment
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For information on CRUD (Create, Read, Update, Delete) operations,
see:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/insert-documents/#std-label-write-op-insert">
Insert Documents
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-documents/#std-label-read-operations-queries">
Query Documents
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/update-documents/#std-label-write-op-update">
Update Documents
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/remove-documents/#std-label-write-op-delete">
Delete Documents
</a>
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
If You Did Not Install MongoDB as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#if-you-did-not-install-mongodb-as-a-windows-service" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="if-you-did-not-install-mongodb-as-a-windows-service">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If you only installed the executables and did not install MongoDB as a
Windows service, you must manually start the MongoDB instance.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-run-mongodb-enterprise-from-cmd" target="_self">
<span>
Start MongoDB Enterprise Edition from the Command Interpreter
</span>
</a>
for instructions to start a
MongoDB instance.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-run-mongodb-enterprise-from-cmd">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Start MongoDB Enterprise Edition from the Command Interpreter
<a class="headerlink leafygreen-ui-1kepx47" href="#start-mongodb-enterprise-edition-from-the-command-interpreter" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-mongodb-enterprise-edition-from-the-command-interpreter">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Create database directory.Create the data directory where MongoDB stores data.\nMongoDB's default data directory path is the absolute path\n\\data\\db on the drive from which you start MongoDB.From the Command Interpreter, create the data directories:"},{"@type":"HowToStep","text":"Start your MongoDB database.To start MongoDB, run mongod.exe.The --dbpath option points to your\ndatabase directory.If the MongoDB database server is running correctly, the\nCommand Interpreter displays:Depending on the\nWindows Defender Firewall\nsettings on your Windows host, Windows may display a\nSecurity Alert dialog box about blocking\n\"some features\" of C:\\Program Files\\MongoDB\\Server\\8.0\\bin\\mongod.exe\nfrom communicating on networks. To remedy this issue:Click Private Networks, such as my home or work\nnetwork.Click Allow access.To learn more about security and MongoDB, see the\nSecurity Documentation."},{"@type":"HowToStep","text":"Connect to MongoDB.If you have not already done so, follow the\nmongosh installation instructions to download\nand install the MongoDB Shell (mongosh).Be sure to add the path to your mongosh.exe binary to your\nPATH environment variable during installation.Open a new Command Interpreter and enter mongosh.exe\nto connect to MongoDB.For more information on connecting to mongod using\nmongosh.exe, such as connecting to a MongoDB instance\nrunning on a different host and/or port, see\nConnect to a Deployment.For information on CRUD (Create, Read, Update, Delete) operations,\nsee:Insert DocumentsQuery DocumentsUpdate DocumentsDelete Documents"}],"name":"Start MongoDB Enterprise Edition from the Command Interpreter","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create database directory.
<a class="headerlink leafygreen-ui-1kepx47" href="#create-database-directory" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-database-directory">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-dbpath">
data directory
</a>
where MongoDB stores data.
MongoDB's default data directory path is the absolute path
<code class="leafygreen-ui-1l06pbn">
\data\db
</code>
on the drive from which you start MongoDB.
</p>
<p class="leafygreen-ui-1kp3ins">
From the
<span class="guilabel css-h15tq0">
Command Interpreter
</span>
, create the data directories:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cd C:\\\nmd \"\\data\\db\""}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cd C:\</td></tr><tr class=""><td class="leafygreen-ui-7razhx">md "\data\db"</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start your MongoDB database.
<a class="headerlink leafygreen-ui-1kepx47" href="#start-your-mongodb-database" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-your-mongodb-database">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To start MongoDB, run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod.exe/#mongodb-binary-bin.mongod.exe">
<code class="leafygreen-ui-1l06pbn">
mongod.exe
</code>
.
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"C:\\Program Files\\MongoDB\\Server\\8.0\\bin\\mongod.exe\" --dbpath=\"c:\\data\\db\""}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">"C:\Program Files\MongoDB\Server\8.0\bin\mongod.exe" --dbpath="c:\data\db"</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
option points to your
database directory.
</p>
<p class="leafygreen-ui-1kp3ins">
If the MongoDB database server is running correctly, the
<span class="guilabel css-h15tq0">
Command Interpreter
</span>
displays:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[initandlisten] waiting for connections","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[initandlisten] waiting <span class="lg-highlight-keyword">for</span> connections</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Depending on the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://learn.microsoft.com/en-us/windows/security/operating-system-security/network-security/windows-firewall/" rel="noopener noreferrer">
<span>
Windows Defender Firewall
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
settings on your Windows host, Windows may display a
<span class="guilabel css-h15tq0">
Security Alert
</span>
dialog box about blocking
"some features" of
<code class="leafygreen-ui-1l06pbn">
C:\Program Files\MongoDB\Server\8.0\bin\mongod.exe
</code>
from communicating on networks. To remedy this issue:
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Click
<span class="guilabel css-h15tq0">
Private Networks, such as my home or work
network
</span>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Click
<span class="guilabel css-h15tq0">
Allow access
</span>
.
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
To learn more about security and MongoDB, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/security/#std-label-security">
Security Documentation
<!-- -->
.
</a>
</p>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Connect to MongoDB.
<a class="headerlink leafygreen-ui-1kepx47" href="#connect-to-mongodb" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="connect-to-mongodb">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If you have not already done so, follow the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/install/" target="_self">
<span>
mongosh installation instructions
</span>
</a>
to download
and install the MongoDB Shell (
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
).
</p>
<p class="leafygreen-ui-1kp3ins">
Be sure to add the path to your
<code class="leafygreen-ui-1l06pbn">
mongosh.exe
</code>
binary to your
<code class="leafygreen-ui-1l06pbn">
PATH
</code>
environment variable during installation.
</p>
<p class="leafygreen-ui-1kp3ins">
Open a new
<span class="guilabel css-h15tq0">
Command Interpreter
</span>
and enter
<code class="leafygreen-ui-1l06pbn">
mongosh.exe
</code>
to connect to MongoDB.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on connecting to
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh.exe
</span>
</a>
, such as connecting to a MongoDB instance
running on a different host and/or port, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/connect/" target="_self">
<span>
Connect to a Deployment
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For information on CRUD (Create, Read, Update, Delete) operations,
see:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/insert-documents/">
Insert Documents
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-documents/">
Query Documents
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/update-documents/">
Update Documents
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/remove-documents/">
Delete Documents
</a>
</p>
</li>
</ul>
</section>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-manually-create-windows-service-enterprise">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Start MongoDB Enterprise Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#start-mongodb-enterprise-edition-as-a-windows-service" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-mongodb-enterprise-edition-as-a-windows-service">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can install and configure MongoDB as a
<span class="guilabel css-h15tq0">
Windows Service
</span>
during
the install, the MongoDB service starts upon successful installation.
</p>
<p class="leafygreen-ui-1kp3ins">
To start/restart the MongoDB service, use the Services console:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
From the Services console, locate the MongoDB service.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Right-click on the MongoDB service and click
<span class="guilabel css-h15tq0">
Start
</span>
.
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
You can also manually manage the service from the command line. To
start the MongoDB service from the command line, open a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd" rel="noopener noreferrer">
<span>
Windows
command prompt/interpreter
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
(
<code class="leafygreen-ui-1l06pbn">
cmd.exe
</code>
) as an
<span class="guilabel css-h15tq0">
Administrator
</span>
, and
run the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Start the MongoDB service.Close all other command prompts, then invoke the following command:"},{"@type":"HowToStep","text":"Verify that MongoDB has started successfully.Check your MongoDB log file for the following line:You may see non-critical warnings in the process\noutput. As long as you see this message in the MongoDB log, you can\nsafely ignore these warnings during your initial evaluation of\nMongoDB."},{"@type":"HowToStep","text":"Connect to the MongoDB server.If you have not already done so, follow the\nmongosh installation instructions to download\nand install the MongoDB Shell (mongosh).Be sure to add the path to your mongosh.exe binary to your\nPATH environment variable during installation.Open a new Command Interpreter and enter mongosh.exe\nto connect to MongoDB."}],"name":"Start MongoDB Enterprise Edition as a Windows Service","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start the MongoDB service.
<a class="headerlink leafygreen-ui-1kepx47" href="#start-the-mongodb-service" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="start-the-mongodb-service">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Close all other command prompts, then invoke the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"net start MongoDB"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">net start MongoDB</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Verify that MongoDB has started successfully.
<a class="headerlink leafygreen-ui-1kepx47" href="#verify-that-mongodb-has-started-successfully" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="verify-that-mongodb-has-started-successfully">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Check your MongoDB log file for the following line:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[initandlisten] waiting for connections on port 27017\n"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1agdxuk" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[initandlisten] waiting for connections on port 27017</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You may see non-critical warnings in the process
output. As long as you see this message in the MongoDB log, you can
safely ignore these warnings during your initial evaluation of
MongoDB.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Connect to the MongoDB server.
<a class="headerlink leafygreen-ui-1kepx47" href="#connect-to-the-mongodb-server" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="connect-to-the-mongodb-server">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If you have not already done so, follow the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/install/" target="_self">
<span>
mongosh installation instructions
</span>
</a>
to download
and install the MongoDB Shell (
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
).
</p>
<p class="leafygreen-ui-1kp3ins">
Be sure to add the path to your
<code class="leafygreen-ui-1l06pbn">
mongosh.exe
</code>
binary to your
<code class="leafygreen-ui-1l06pbn">
PATH
</code>
environment variable during installation.
</p>
<p class="leafygreen-ui-1kp3ins">
Open a new
<span class="guilabel css-h15tq0">
Command Interpreter
</span>
and enter
<code class="leafygreen-ui-1l06pbn">
mongosh.exe
</code>
to connect to MongoDB.
</p>
</section>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Stop MongoDB Enterprise Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-mongodb-enterprise-edition-as-a-windows-service" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stop-mongodb-enterprise-edition-as-a-windows-service">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To stop/pause the MongoDB service, use the Services console:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
From the Services console, locate the MongoDB service.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Right-click on the MongoDB service and click
<span class="guilabel css-h15tq0">
Stop
</span>
(or
<span class="guilabel css-h15tq0">
Pause
</span>
).
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
You can also manage the service from the command line. To stop the
MongoDB service from the command line, open a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd" rel="noopener noreferrer">
<span>
Windows command
prompt/interpreter
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
(
<code class="leafygreen-ui-1l06pbn">
cmd.exe
</code>
) as an
<span class="guilabel css-h15tq0">
Administrator
</span>
, and
run the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"net stop MongoDB"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">net stop MongoDB</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Remove MongoDB Enterprise Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#remove-mongodb-enterprise-edition-as-a-windows-service" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="remove-mongodb-enterprise-edition-as-a-windows-service">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To remove the MongoDB service, first use the Services console to stop
the service. Then open a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd" rel="noopener noreferrer">
<span>
Windows command prompt/interpreter
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
(
<code class="leafygreen-ui-1l06pbn">
cmd.exe
</code>
) as an
<span class="guilabel css-h15tq0">
Administrator
</span>
, and
run the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sc.exe delete MongoDB"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">sc.exe delete MongoDB</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Additional Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#additional-considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="additional-considerations">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Localhost Binding by Default
<a class="headerlink leafygreen-ui-1kepx47" href="#localhost-binding-by-default" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="localhost-binding-by-default">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
By default, MongoDB launches with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.bindIp">
<code class="leafygreen-ui-1l06pbn">
bindIp
</code>
</a>
set to
<code class="leafygreen-ui-1l06pbn">
127.0.0.1
</code>
, which binds to the localhost network interface. This
means that the
<code class="leafygreen-ui-1l06pbn">
mongod.exe
</code>
can only accept connections from
clients that are running on the same machine. Remote clients will not be
able to connect to the
<code class="leafygreen-ui-1l06pbn">
mongod.exe
</code>
, and the
<code class="leafygreen-ui-1l06pbn">
mongod.exe
</code>
will
not be able to initialize a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
</a>
unless this value is set
to a valid network interface.
</p>
<p class="leafygreen-ui-1kp3ins">
This value can be configured either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
in the MongoDB configuration file with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.bindIp">
<code class="leafygreen-ui-1l06pbn">
bindIp
</code>
</a>
, or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
via the command-line argument
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--bind_ip">
<code class="leafygreen-ui-1l06pbn">
--bind_ip
</code>
</a>
</p>
</li>
</ul>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before you bind your instance to a publicly-accessible IP address,
you must secure your cluster from unauthorized access. For a complete
list of security recommendations, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-security-checklist">
Security Checklist for Self-Managed Deployments
</a>
. At minimum, consider
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-checklist-auth">
enabling authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-hardening/#std-label-network-config-hardening">
hardening
network infrastructure
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on configuring
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.bindIp">
<code class="leafygreen-ui-1l06pbn">
bindIp
</code>
</a>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-mongodb-configuration/">
IP Binding in Self-Managed Deployments
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Point Releases and
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#point-releases-and-.msi" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="point-releases-and-.msi">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If you installed MongoDB with the Windows installer (
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
), that
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
automatically upgrades within its
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/versioning/#std-label-release-version-numbers">
release series
</a>
(e.g. 7.2.1 to 7.2.2).
</p>
<p class="leafygreen-ui-1kp3ins">
Upgrading a full release series (e.g. 6.0 to 7.0) requires a new
installation.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Add MongoDB binaries to the System PATH
<a class="headerlink leafygreen-ui-1kepx47" href="#add-mongodb-binaries-to-the-system-path" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="add-mongodb-binaries-to-the-system-path">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
All command-line examples in this tutorial are provided with absolute
paths to the MongoDB binaries. You can add
<code class="leafygreen-ui-1l06pbn">
C:\Program
Files\MongoDB\Server\8.0\bin
</code>
to your System
<code class="leafygreen-ui-1l06pbn">
PATH
</code>
and then
omit the full path to the MongoDB binaries.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/objectToArray/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$objectToArray (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-objecttoarray--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-objecttoarray--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-objectToArray" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-objectToArray">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Converts a document to an array. The return array contains an
element for each field/value pair in the original document.
Each element in the return array is a document that contains two
fields
<code class="leafygreen-ui-1l06pbn">
k
</code>
and
<code class="leafygreen-ui-1l06pbn">
v
</code>
:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
k
</code>
field contains the field name in the original document.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
v
</code>
field contains the value of the field in the original
document.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-objectToArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
</span>
</a>
has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $objectToArray: <object> }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$objectToArray</span>: <<!-- -->object<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
<object>
</code>
expression can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
as long as it resolves to a document
object.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-objectToArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
</span>
</a>
applies to the top-level fields
of its argument. If the argument is a document that itself contains
embedded document fields, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-objectToArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
</span>
</a>
does not
recursively apply to the embedded document fields.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:65%"/>
<col style="width:35%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Results
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $objectToArray: { item: \"foo\", qty: 25 } }","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1agdxuk" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span> $objectToArray<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span> item<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"foo"</span><span class="lg-highlight-punctuation">,</span> qty<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">25</span> <span class="lg-highlight-punctuation">}</span> <span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n \"k\" : \"item\",\n \"v\" : \"foo\"\n },\n {\n \"k\" : \"qty\",\n \"v\" : 25\n }\n]","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">[</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"k"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"item"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"v"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"foo"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"k"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"qty"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"v"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">25</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">]</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $objectToArray: {\n item: \"foo\",\n qty: 25,\n size: { len: 25, w: 10, uom: \"cm\" }\n } }","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span> $objectToArray<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> item<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"foo"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> qty<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">25</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> size<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span> len<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">25</span><span class="lg-highlight-punctuation">,</span> w<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">10</span><span class="lg-highlight-punctuation">,</span> uom<span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"cm"</span> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span> <span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n \"k\" : \"item\",\n \"v\" : \"foo\"\n },\n {\n \"k\" : \"qty\",\n \"v\" : 25\n },\n {\n \"k\" : \"size\",\n \"v\" : {\n \"len\" : 25,\n \"w\" : 10,\n \"uom\" : \"cm\"\n }\n }\n]","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">[</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"k"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"item"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"v"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"foo"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"k"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"qty"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"v"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">25</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"k"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"size"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"v"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"len"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">25</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"w"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">10</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"uom"</span> <span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"cm"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">]</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#-objecttoarray-example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-objecttoarray-example">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Consider a
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"item\" : \"ABC1\", dimensions: { l: 25, w: 10, uom: \"cm\" } }\n{ \"_id\" : 2, \"item\" : \"ABC2\", dimensions: { l: 50, w: 25, uom: \"cm\" } }\n{ \"_id\" : 3, \"item\" : \"XYZ1\", dimensions: { l: 70, w: 75, uom: \"cm\" } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC1"</span>, <span class="lg-highlight-attr">dimensions</span>: <!-- -->{ <span class="lg-highlight-attr">l</span>: <span class="lg-highlight-number">25</span>, <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">uom</span>: <span class="lg-highlight-string">"cm"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC2"</span>, <span class="lg-highlight-attr">dimensions</span>: <!-- -->{ <span class="lg-highlight-attr">l</span>: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-number">25</span>, <span class="lg-highlight-attr">uom</span>: <span class="lg-highlight-string">"cm"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"XYZ1"</span>, <span class="lg-highlight-attr">dimensions</span>: <!-- -->{ <span class="lg-highlight-attr">l</span>: <span class="lg-highlight-number">70</span>, <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-attr">uom</span>: <span class="lg-highlight-string">"cm"</span> } }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation pipeline operation use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-objectToArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
</span>
</a>
to return the
<code class="leafygreen-ui-1l06pbn">
dimensions
</code>
field as an array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.aggregate(\n [\n {\n $project: {\n item: 1,\n dimensions: { $objectToArray: \"$dimensions\" }\n }\n }\n ]\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$project</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">dimensions</span>: <!-- -->{ <span class="lg-highlight-attr">$objectToArray</span>: <span class="lg-highlight-string">"$dimensions"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"item\" : \"ABC1\", \"dimensions\" : [ { \"k\" : \"l\", \"v\" : 25 }, { \"k\" : \"w\", \"v\" : 10 }, { \"k\" : \"uom\", \"v\" : \"cm\" } ] }\n{ \"_id\" : 2, \"item\" : \"ABC2\", \"dimensions\" : [ { \"k\" : \"l\", \"v\" : 50 }, { \"k\" : \"w\", \"v\" : 25 }, { \"k\" : \"uom\", \"v\" : \"cm\" } ] }\n{ \"_id\" : 3, \"item\" : \"XYZ1\", \"dimensions\" : [ { \"k\" : \"l\", \"v\" : 70 }, { \"k\" : \"w\", \"v\" : 75 }, { \"k\" : \"uom\", \"v\" : \"cm\" } ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC1"</span>, <span class="lg-highlight-string">"dimensions"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"l"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-number">25</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"w"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-number">10</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"uom"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-string">"cm"</span> } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC2"</span>, <span class="lg-highlight-string">"dimensions"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"l"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-number">50</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"w"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-number">25</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"uom"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-string">"cm"</span> } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"XYZ1"</span>, <span class="lg-highlight-string">"dimensions"</span> <!-- -->: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"l"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-number">70</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"w"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-number">75</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-string">"k"</span> <!-- -->: <span class="lg-highlight-string">"uom"</span>, <span class="lg-highlight-string">"v"</span> <!-- -->: <span class="lg-highlight-string">"cm"</span> } ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
to Sum Nested Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#-objecttoarray-to-sum-nested-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-objecttoarray-to-sum-nested-fields">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Consider a
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"item\" : \"ABC1\", instock: { warehouse1: 2500, warehouse2: 500 } }\n{ \"_id\" : 2, \"item\" : \"ABC2\", instock: { warehouse2: 500, warehouse3: 200} }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC1"</span>, <span class="lg-highlight-attr">instock</span>: <!-- -->{ <span class="lg-highlight-attr">warehouse1</span>: <span class="lg-highlight-number">2500</span>, <span class="lg-highlight-attr">warehouse2</span>: <span class="lg-highlight-number">500</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC2"</span>, <span class="lg-highlight-attr">instock</span>: <!-- -->{ <span class="lg-highlight-attr">warehouse2</span>: <span class="lg-highlight-number">500</span>, <span class="lg-highlight-attr">warehouse3</span>: <span class="lg-highlight-number">200</span>} }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation pipeline operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-objectToArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
</span>
</a>
along with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unwind/#mongodb-pipeline-pipe.-unwind">
<code class="leafygreen-ui-1l06pbn">
$unwind
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
to calculate the total items in stock per warehouse.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.aggregate([\n { $project: { warehouses: { $objectToArray: \"$instock\" } } },\n { $unwind: \"$warehouses\" },\n { $group: { _id: \"$warehouses.k\", total: { $sum: \"$warehouses.v\" } } }\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">warehouses</span>: <!-- -->{ <span class="lg-highlight-attr">$objectToArray</span>: <span class="lg-highlight-string">"$instock"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$unwind</span>: <span class="lg-highlight-string">"$warehouses"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$group</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$warehouses.k"</span>, <span class="lg-highlight-attr">total</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$warehouses.v"</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"warehouse3\", \"total\" : 200 }\n{ \"_id\" : \"warehouse2\", \"total\" : 1000 }\n{ \"_id\" : \"warehouse1\", \"total\" : 2500 }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"warehouse3"</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">200</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"warehouse2"</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">1000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"warehouse1"</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">2500</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
+
<code class="leafygreen-ui-1l06pbn">
$arrayToObject
</code>
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#-objecttoarray----arraytoobject-example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-objecttoarray----arraytoobject-example">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Consider a
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"item\" : \"ABC1\", instock: { warehouse1: 2500, warehouse2: 500 } }\n{ \"_id\" : 2, \"item\" : \"ABC2\", instock: { warehouse2: 500, warehouse3: 200} }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC1"</span>, <span class="lg-highlight-attr">instock</span>: <!-- -->{ <span class="lg-highlight-attr">warehouse1</span>: <span class="lg-highlight-number">2500</span>, <span class="lg-highlight-attr">warehouse2</span>: <span class="lg-highlight-number">500</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC2"</span>, <span class="lg-highlight-attr">instock</span>: <!-- -->{ <span class="lg-highlight-attr">warehouse2</span>: <span class="lg-highlight-number">500</span>, <span class="lg-highlight-attr">warehouse3</span>: <span class="lg-highlight-number">200</span>} }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following aggregation pipeline operation calculates the total in
stock for each item and adds to the
<code class="leafygreen-ui-1l06pbn">
instock
</code>
document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.aggregate( [\n { $addFields: { instock: { $objectToArray: \"$instock\" } } },\n { $addFields: { instock: { $concatArrays: [ \"$instock\", [ { \"k\": \"total\", \"v\": { $sum: \"$instock.v\" } } ] ] } } } ,\n { $addFields: { instock: { $arrayToObject: \"$instock\" } } }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$addFields</span>: <!-- -->{ <span class="lg-highlight-attr">instock</span>: <!-- -->{ <span class="lg-highlight-attr">$objectToArray</span>: <span class="lg-highlight-string">"$instock"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$addFields</span>: <!-- -->{ <span class="lg-highlight-attr">instock</span>: <!-- -->{ <span class="lg-highlight-attr">$concatArrays</span>: <!-- -->[ <span class="lg-highlight-string">"$instock"</span>, <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"k"</span>: <span class="lg-highlight-string">"total"</span>, <span class="lg-highlight-string">"v"</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$instock.v"</span> } } ] ] } } } <!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$addFields</span>: <!-- -->{ <span class="lg-highlight-attr">instock</span>: <!-- -->{ <span class="lg-highlight-attr">$arrayToObject</span>: <span class="lg-highlight-string">"$instock"</span> } } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"item\" : \"ABC1\", \"instock\" : { \"warehouse1\" : 2500, \"warehouse2\" : 500, \"total\" : 3000 } }\n{ \"_id\" : 2, \"item\" : \"ABC2\", \"instock\" : { \"warehouse2\" : 500, \"warehouse3\" : 200, \"total\" : 700 } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC1"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"warehouse1"</span> <!-- -->: <span class="lg-highlight-number">2500</span>, <span class="lg-highlight-string">"warehouse2"</span> <!-- -->: <span class="lg-highlight-number">500</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">3000</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"ABC2"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"warehouse2"</span> <!-- -->: <span class="lg-highlight-number">500</span>, <span class="lg-highlight-string">"warehouse3"</span> <!-- -->: <span class="lg-highlight-number">200</span>, <span class="lg-highlight-string">"total"</span> <!-- -->: <span class="lg-highlight-number">700</span> } }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/arrayToObject/#mongodb-expression-exp.-arrayToObject">
<code class="leafygreen-ui-1l06pbn">
$arrayToObject
</code>
</a>
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/security-ldap-external/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
LDAP Authorization on Self-Managed Deployments
<a class="headerlink leafygreen-ui-1kepx47" href="#ldap-authorization-on-self-managed-deployments" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="ldap-authorization-on-self-managed-deployments">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#considerations" target="_self">
<span>
Considerations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#configuration" target="_self">
<span>
Configuration
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#tutorials" target="_self">
<span>
Tutorials
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#connecting-to-a-mongodb-server-using-ldap-authorization" target="_self">
<span>
Connecting to a MongoDB server using LDAP Authorization
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#mongodb-roles-for-ldap-authorization" target="_self">
<span>
MongoDB Roles for LDAP Authorization
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, LDAP authentication and authorization is
deprecated. LDAP is available and will continue to operate without
changes throughout the lifetime of MongoDB 8. LDAP will be removed in a
future major release.
</p>
<p class="leafygreen-ui-1kp3ins">
For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/LDAP-deprecation/#std-label-ldap-deprecation">
LDAP Deprecation
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://www.mongodb.com/products/mongodb-enterprise-advanced?tck=docs_server" target="_self">
<span>
MongoDB Enterprise
</span>
</a>
supports
querying an LDAP server for the LDAP groups to which the authenticated user
belongs. MongoDB maps the distinguished names (DN) of each returned group
to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-roles">
roles
</a>
on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database. MongoDB authorizes the
user based on the mapped roles and their associated privileges. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-security-ldap-external" target="_self">
<span>
LDAP Authorization
</span>
</a>
for more information.
</p>
<p class="leafygreen-ui-1kp3ins">
The LDAP Authorization process is summarized below:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A client connects to MongoDB and performs authentication with any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authentication/#std-label-authentication">
authentication
</a>
mechanism that
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-security-ldap-external-compatibility" target="_self">
<span>
supports external authentication
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-isolation-consistency-recency/#std-label-sessions">
Client Sessions and Causal Consistency Guarantees
</a>
with
<code class="leafygreen-ui-1l06pbn">
$external
</code>
authentication users
(Kerberos, LDAP, or x.509 users), usernames cannot be greater
than 10k bytes.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB binds to the LDAP server specified with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.servers">
<code class="leafygreen-ui-1l06pbn">
security.ldap.servers
</code>
</a>
using the credentials specified with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryUser">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.queryUser
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryPassword">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.queryPassword
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB uses simple binding by default, but can use
<code class="leafygreen-ui-1l06pbn">
sasl
</code>
binding
instead if configured in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.method">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.method
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.saslMechanisms">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.saslMechanisms
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB constructs an LDAP query using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.authz.queryTemplate">
<code class="leafygreen-ui-1l06pbn">
security.ldap.authz.queryTemplate
</code>
</a>
and queries the LDAP server for
the authenticated user's group membership.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB can use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.userToDNMapping">
<code class="leafygreen-ui-1l06pbn">
security.ldap.userToDNMapping
</code>
</a>
option to
transform the username for supporting the query template.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The LDAP server evaluates the query and returns the list of
groups to which the authenticated user belongs.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB authorizes the user to perform actions on the server by mapping
each returned group's Distinguished Name (DN) into a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-authorization">
role
</a>
on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database. If a returned group DN exactly
matches the name of an existing role on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database, MongoDB
grants the user the roles and privileges assigned to that role. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-security-ldap-external-roles" target="_self">
<span>
MongoDB Roles for LDAP Authorization
</span>
</a>
for more information.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The client can perform actions on the MongoDB server which require the roles
or privileges granted to the authenticated user.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
At an interval defined by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.ldapUserCacheInvalidationInterval">
<code class="leafygreen-ui-1l06pbn">
ldapUserCacheInvalidationInterval
</code>
</a>
,
MongoDB flushes the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
cache. Prior to executing
subsequent operations performed by externally authorized users, MongoDB
re-acquires their group membership from the LDAP server.
</p>
</li>
</ol>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="considerations">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
A full description of LDAP is beyond the scope of this documentation. This
page assumes prior knowledge of LDAP.
</p>
<p class="leafygreen-ui-1kp3ins">
This documentation only describes MongoDB LDAP authorization, and does not
replace other resources on LDAP. We encourage you to thoroughly familiarize
yourself with LDAP and its related subject matter before configuring LDAP
authentication.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB can provide
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/consulting?tck=docs_server" target="_self">
<span>
professional services
</span>
</a>
for optimal
configuration of LDAP authorization for your MongoDB deployment.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-security-ldap-external-compatibility">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Compatible Authentication Mechanism
<a class="headerlink leafygreen-ui-1kepx47" href="#compatible-authentication-mechanism" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatible-authentication-mechanism">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB supports LDAP authorization with the following authentication methods:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/#std-label-security-ldap">
Self-Managed LDAP Proxy Authentication
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/kerberos/#std-label-security-kerberos">
Kerberos Authentication on Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-x.509/#std-label-security-auth-x509">
x.509
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
With this configuration, MongoDB uses LDAP, X.509, or Kerberos
authorization to authenticate client connections.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-ldap-connection-pool-behavior">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Connection Pool
<a class="headerlink leafygreen-ui-1kepx47" href="#connection-pool" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="connection-pool">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When connecting to the LDAP server for authentication/authorization, MongoDB,
by default:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses connection pooling if run:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
on Windows or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
on Linux where MongoDB Enterprise binaries are linked against
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/#std-label-libldap-vs-libldap_r">
libldap_r
<!-- -->
.
</a>
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Does not use connection pooling if run:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
on Linux where MongoDB Enterprise binaries are linked against
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/#std-label-libldap-vs-libldap_r">
libldap
<!-- -->
.
</a>
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To change the connection pooling behavior, update the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.ldapUseConnectionPool">
<code class="leafygreen-ui-1l06pbn">
ldapUseConnectionPool
</code>
</a>
parameter.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
libldap
</code>
and
<code class="leafygreen-ui-1l06pbn">
libldap_r
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#libldap-and-libldap_r" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="libldap-and-libldap_r">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For MongoDB 4.2 Enterprise binaries linked against
<code class="leafygreen-ui-1l06pbn">
libldap
</code>
(such as when running on RHEL), access to the
<code class="leafygreen-ui-1l06pbn">
libldap
</code>
is synchronized, incurring some performance/latency
costs.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB 4.2 Enterprise binaries linked against
<code class="leafygreen-ui-1l06pbn">
libldap_r
</code>
, there is no change in behavior from earlier MongoDB
versions.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
User Management
<a class="headerlink leafygreen-ui-1kepx47" href="#user-management" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="user-management">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
With LDAP authorization, user creation and management occurs on the LDAP
server. MongoDB requires creation of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-roles">
roles
</a>
on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database, with the name of each role exactly matching a LDAP group
Distinguished Name (DN). This is in contrast to MongoDB managed authorization,
which requires creating users on the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database.
</p>
<p class="leafygreen-ui-1kp3ins">
To manage roles on the MongoDB server, authenticate as a user whose group
membership corresponds to a
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database role with role administration
privileges, such as those provided by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-userAdmin">
<code class="leafygreen-ui-1l06pbn">
userAdmin
</code>
</a>
. Create or update
roles corresponding to LDAP group DNs such that users with membership in that
group receive the appropriate roles and privileges.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, an LDAP group for database administrators might have a role with
administrative roles and privileges. An LDAP group for marketing or analytics
users may have a role with only have read privileges on certain databases.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
When configuring a role for a corresponding LDAP Group, remember that
<em>
all
</em>
users with membership in that group can receive the configured roles and
privileges. Consider applying the principle of least privilege when
configuring MongoDB roles, LDAP groups, or group membership.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If no role with role administration privileges exists
<em>
AND
</em>
no
non-
<code class="leafygreen-ui-1l06pbn">
$external
</code>
user with these privileges exists, you effectively cannot
perform user management, as no new or existing roles can be altered to reflect
additions or changes to groups or group membership on the LDAP server.
</p>
<p class="leafygreen-ui-1kp3ins">
To remedy a scenario where you cannot manage roles on the MongoDB server,
perform the following procedure:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart the MongoDB server without authentication and LDAP authorization
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create a role on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database whose name corresponds to the
appropriate LDAP group Distinguished Name. When choosing a group DN,
consider which group is most appropriate for database administration.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart the MongoDB server with authentication and LDAP authorization
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Authenticate as a user with membership in the group corresponding to the
created administrative role.
</p>
</li>
</ol>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Existing Users
<a class="headerlink leafygreen-ui-1kepx47" href="#existing-users" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="existing-users">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
A MongoDB server using LDAP for authorization makes any existing users on the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database inaccessible. If there are existing users in
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database, you must meet the following requirements for each user
on the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database to ensure continued access:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
User has a corresponding user object on the LDAP server
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
User object has membership in the appropriate LDAP groups
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB has roles on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database named for the user's LDAP
groups, such that the granted roles and privileges are identical to those
granted to the non-
<code class="leafygreen-ui-1l06pbn">
$external
</code>
user.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If you want to continue allowing access by users
<em>
not
</em>
on the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database, ensure the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.authenticationMechanisms">
<code class="leafygreen-ui-1l06pbn">
authenticationMechanisms
</code>
</a>
parameter includes
<code class="leafygreen-ui-1l06pbn">
SCRAM-SHA-1
</code>
and/or
<code class="leafygreen-ui-1l06pbn">
SCRAM-SHA-256
</code>
as appropriate. Alternatively,
apply the requirements listed above for transitioning those users to
LDAP authorization.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Replica Sets
<a class="headerlink leafygreen-ui-1kepx47" href="#replica-sets" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="replica-sets">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica sets
</a>
, configure LDAP authorization on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-arbiter">
arbiter
</a>
members first before configuring the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
. This also applies to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-shards/#std-label-shards-concepts">
shard replica sets
</a>
, or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-config-servers/#std-label-csrs">
config server replica sets
</a>
.
Configure one replica set member at a time to maintain a majority of members
for write availability.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sharded Clusters
<a class="headerlink leafygreen-ui-1kepx47" href="#sharded-clusters" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sharded-clusters">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded clusters
</a>
, you must configure LDAP
authorization on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-server">
config servers
</a>
for cluster-level
users. You can optionally configure LDAP authorization on each
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shard
</a>
for shard-local users.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Configuration
<a class="headerlink leafygreen-ui-1kepx47" href="#configuration" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="configuration">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You must configure the following settings to use LDAP Authorization:
</p>
<p class="leafygreen-ui-1kp3ins">
To use LDAP for authorization via operating system libraries, specify the
following settings as a part of your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
configuration file:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:60%"/>
<col style="width:10%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Option
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Required
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.servers">
<code class="leafygreen-ui-1l06pbn">
security.ldap.servers
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Quote-enclosed comma-separated list of LDAP servers in
<code class="leafygreen-ui-1l06pbn">
host[:port]
</code>
format.
</p>
<p class="leafygreen-ui-1kp3ins">
You can prefix LDAP servers with
<code class="leafygreen-ui-1l06pbn">
srv:
</code>
and
<code class="leafygreen-ui-1l06pbn">
srv_raw:
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If your connection string specifies
<code class="leafygreen-ui-1l06pbn">
"srv:<DNS_NAME>"
</code>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
verifies that
<code class="leafygreen-ui-1l06pbn">
"_ldap._tcp.gc._msdcs.<DNS_NAME>"
</code>
exists for SRV to
support Active Directory. If not found,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
verifies that
<code class="leafygreen-ui-1l06pbn">
"_ldap._tcp.<DNS_NAME>"
</code>
exists for SRV. If an SRV record cannot be
found,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
warns you to use
<code class="leafygreen-ui-1l06pbn">
"srv_raw:<DNS_NAME>"
</code>
instead.
</p>
<p class="leafygreen-ui-1kp3ins">
If your connection string specifies
<code class="leafygreen-ui-1l06pbn">
"srv_raw:<DNS_NAME>"
</code>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
performs an SRV record lookup for
<code class="leafygreen-ui-1l06pbn">
"<DNS NAME>"
</code>
.
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<strong>
YES
</strong>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.authz.queryTemplate">
<code class="leafygreen-ui-1l06pbn">
security.ldap.authz.queryTemplate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
An
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://tools.ietf.org/html/rfc4515" rel="noopener noreferrer">
<span>
RFC4515
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://tools.ietf.org/html/rfc4516" rel="noopener noreferrer">
<span>
RFC4516
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
LDAP formatted query URL
template executed by MongoDB to obtain the LDAP groups to
which the user belongs to. The query is relative to the host or hosts
specified in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.servers">
<code class="leafygreen-ui-1l06pbn">
servers
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
You can use the following tokens in the template:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
<code class="leafygreen-ui-1l06pbn">
{USER}
</code>
</dt>
<dd class="css-1ufxuu7">
Substitutes the authenticated username, or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.userToDNMapping">
<code class="leafygreen-ui-1l06pbn">
transformed
</code>
</a>
username, into the LDAP query.
</dd>
</dl>
</li>
<li class="leafygreen-ui-d7p7b6">
<dl>
<dt>
<code class="leafygreen-ui-1l06pbn">
{PROVIDED_USER}
</code>
</dt>
<dd class="css-1ufxuu7">
Substitutes the supplied username, i.e. before either
authentication or LDAP transformation, into the LDAP query.
</dd>
</dl>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Only
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
supports this parameter.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
defers to this setting as configured on its
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-server">
config servers
</a>
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<strong>
YES
</strong>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryUser">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.queryUser
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The identity the MongoDB server binds as when connecting to and
executing operations and queries on an LDAP server.
</p>
<p class="leafygreen-ui-1kp3ins">
Use with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryPassword">
<code class="leafygreen-ui-1l06pbn">
queryPassword
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The user specified must have the appropriate privileges to support the
LDAP queries generated from the configured
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.authz.queryTemplate">
<code class="leafygreen-ui-1l06pbn">
queryTemplate
</code>
.
</a>
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<strong>
YES
</strong>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryPassword">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.queryPassword
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The password used to bind to an LDAP server when using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryUser">
<code class="leafygreen-ui-1l06pbn">
queryUser
</code>
.
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<strong>
YES
</strong>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.method">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.method
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Used to specify the method the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
uses to authenticate, or bind, to the LDAP server. Specify
<code class="leafygreen-ui-1l06pbn">
sasl
</code>
to
use one of the SASL protocols defined in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.saslMechanisms">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.saslMechanisms
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Defaults to
<code class="leafygreen-ui-1l06pbn">
simple
</code>
.
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<strong>
NO
</strong>
, unless using
<code class="leafygreen-ui-1l06pbn">
sasl
</code>
for binding to the LDAP server.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.saslMechanisms">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.saslMechanisms
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Used to specify the SASL mechanisms
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
can use when authenticating or binding to the LDAP
server. MongoDB and the LDAP server must agree on at least one SASL
mechanism.
</p>
<p class="leafygreen-ui-1kp3ins">
Defaults to
<code class="leafygreen-ui-1l06pbn">
DIGEST-MD5
</code>
.
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<strong>
NO
</strong>
, unless setting
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.method">
<code class="leafygreen-ui-1l06pbn">
method
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
sasl
</code>
, and you need different or additional SASL mechanisms.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.useOSDefaults">
<code class="leafygreen-ui-1l06pbn">
security.ldap.bind.useOSDefaults
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Windows MongoDB deployments can use the operating system credentials in
place of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryUser">
<code class="leafygreen-ui-1l06pbn">
queryUser
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryPassword">
<code class="leafygreen-ui-1l06pbn">
queryPassword
</code>
</a>
for authenticating or binding
as when connecting to the LDAP server.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<strong>
NO
</strong>
, unless replacing
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryUser">
<code class="leafygreen-ui-1l06pbn">
queryUser
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.bind.queryPassword">
<code class="leafygreen-ui-1l06pbn">
queryPassword
</code>
.
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.userToDNMapping">
<code class="leafygreen-ui-1l06pbn">
security.ldap.userToDNMapping
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Depending on your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.authz.queryTemplate">
<code class="leafygreen-ui-1l06pbn">
queryTemplate
</code>
</a>
,
the authenticated client username may require transformation to
support the LDAP query URL.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.userToDNMapping">
<code class="leafygreen-ui-1l06pbn">
userToDNMapping
</code>
</a>
allows MongoDB to
transform incoming usernames.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<strong>
NO
</strong>
, unless client usernames require transformation into LDAP DNs.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
When you have configured LDAP authorization, restart
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-program-mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-program-mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
. The server now uses LDAP authorization with
X.509, Kerberos, or LDAP to authenticate client connections.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
LDAP Query Template
<a class="headerlink leafygreen-ui-1kepx47" href="#ldap-query-template" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="ldap-query-template">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.authz.queryTemplate">
<code class="leafygreen-ui-1l06pbn">
security.ldap.authz.queryTemplate
</code>
</a>
to create an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://tools.ietf.org/html/rfc4516" rel="noopener noreferrer">
<span>
RFC4516
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
formatted LDAP query URL. In
the template, you can use either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{USER}
</code>
placeholder to substitute the authenticated username into
the LDAP query URL. If MongoDB transformed the username using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.userToDNMapping">
<code class="leafygreen-ui-1l06pbn">
userToDNMapping
</code>
</a>
, MongoDB replaces the
<code class="leafygreen-ui-1l06pbn">
{USER}
</code>
token with the transformed username when constructing the
LDAP query URL.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{PROVIDED_USER}
</code>
placeholder to substitute the supplied username,
i.e. before either authentication or LDAP transformation, into the
LDAP query.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Design the query template to retrieve the user's groups.
</p>
<div class="leafygreen-ui-vg7qwp" role="note">
<h2 class="leafygreen-ui-2xicti">
Example
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The following query template returns any groups listed in the
LDAP user object's
<code class="leafygreen-ui-1l06pbn">
memberOf
</code>
attribute. This query assumes the
<code class="leafygreen-ui-1l06pbn">
memberOf
</code>
attribute exists - your specific LDAP deployment may use a different attribute
or methodology for tracking group membership. This query also assumes
the user authenticates using their full LDAP DN as their username.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"{USER}?memberOf?base\"","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">"{USER}?memberOf?base"</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The LDAP query URL must conform to the format defined in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://tools.ietf.org/html/rfc4516" rel="noopener noreferrer">
<span>
RFC4516
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[ dn [ ? [attributes] [ ? [scope] [ ? [filter] [ ? [Extensions] ] ] ] ] ]"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[ dn [ ? [attributes] [ ? [scope] [ ? [filter] [ ? [Extensions] ] ] ] ] ]</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Consider the definition of each component, as quoted from RFC4516:
</p>
<blockquote>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
dn
</code>
is an LDAP Distinguished Name using the string format described
in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://tools.ietf.org/html/rfc4514" rel="noopener noreferrer">
<span>
RFC4514
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
. It identifies the base
object of the LDAP search or the target of a non-search operation.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
attributes
</code>
construct is used to indicate which attributes should be
returned from the entry or entries.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
scope
</code>
construct is used to specify the scope of the search to perform
in the given LDAP server. The allowable scopes are "base" for a base object
search, "one" for a one-level search, or "sub" for a subtree search.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
filter
</code>
is used to specify the search filter to apply to entries
within the specified scope during the search. It has the format specified
in [RFC4515].
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
extensions
</code>
construct provides the LDAP URL with an extensibility
mechanism, allowing the capabilities of the URL to be extended in the
future.
</p>
</blockquote>
<p class="leafygreen-ui-1kp3ins">
If the query includes an
<code class="leafygreen-ui-1l06pbn">
attribute
</code>
, MongoDB assumes the query retrieves a
the DNs which this entity is member of.
</p>
<p class="leafygreen-ui-1kp3ins">
If the query does not include an attribute, MongoDB assumes the query
retrieves all entities for which the user is member of.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB currently ignores any extensions specified in the LDAP query.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
A full description of RFC4516 or LDAP query URL construction is out of
scope for this documentation.
</p>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Tutorials
<a class="headerlink leafygreen-ui-1kepx47" href="#tutorials" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="tutorials">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following tutorials contain procedures for connecting to an LDAP server
via the Operating System LDAP libraries:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/authenticate-nativeldap-activedirectory/">
Authenticate and Authorize Users Using Self-Managed Active Directory with Native LDAP
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/kerberos-auth-activedirectory-authz/">
Configure Self-Managed MongoDB with Kerberos Authentication and Active Directory Authorization
</a>
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Connecting to a MongoDB server using LDAP Authorization
<a class="headerlink leafygreen-ui-1kepx47" href="#connecting-to-a-mongodb-server-using-ldap-authorization" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="connecting-to-a-mongodb-server-using-ldap-authorization">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When using LDAP for authorization, users connecting via
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
must:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
set
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--authenticationDatabase" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--authenticationDatabase
</code>
</span>
</a>
to
<code class="leafygreen-ui-1l06pbn">
$external
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
set
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--authenticationMechanism" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--authenticationMechanism
</code>
</span>
</a>
to the appropriate authentication
mechanism.
</p>
<p class="leafygreen-ui-1kp3ins">
If using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/#std-label-security-ldap">
LDAP authentication
</a>
, set this to
<code class="leafygreen-ui-1l06pbn">
PLAIN
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/kerberos/#std-label-security-kerberos">
Kerberos authentication
</a>
, set this to
<code class="leafygreen-ui-1l06pbn">
GSSAPI
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-x.509/#std-label-security-auth-x509">
x.509
</a>
, set this to
<code class="leafygreen-ui-1l06pbn">
MONGODB-X.509
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
set
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--username" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--username
</code>
</span>
</a>
to a username that respects the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.authz.queryTemplate">
<code class="leafygreen-ui-1l06pbn">
security.ldap.authz.queryTemplate
</code>
</a>
, or any configured
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.userToDNMapping">
<code class="leafygreen-ui-1l06pbn">
security.ldap.userToDNMapping
</code>
</a>
template.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
set
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--password" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--password
</code>
</span>
</a>
to the appropriate password.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Include the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--host" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--host
</code>
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--port" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--port
</code>
</span>
</a>
of the MongoDB server,
along with any other options relevant to your deployment.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following operation authenticates to a MongoDB server running
with LDAP authentication and authorization:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --username [email protected] --password --authenticationDatabase '$external' --authenticationMechanism \"PLAIN\" --host \"mongodb.example.com\" --port 27017","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongosh --username [email protected] --password --authenticationDatabase <span class="lg-highlight-string">'$external'</span> --authenticationMechanism <span class="lg-highlight-string">"PLAIN"</span> --host <span class="lg-highlight-string">"mongodb.example.com"</span> --port 27017</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you do not specify the password to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--password" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--password
</code>
</span>
</a>
command-line option,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
prompts for the password.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
$external
</code>
argument must be placed in single quotes, not
double quotes, to prevent the shell from interpreting
<code class="leafygreen-ui-1l06pbn">
$external
</code>
as a variable.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-security-ldap-external-roles">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
MongoDB Roles for LDAP Authorization
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-roles-for-ldap-authorization" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-roles-for-ldap-authorization">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB maps each returned group distinguished name (DN) returned by
the LDAP
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.authz.queryTemplate">
<code class="leafygreen-ui-1l06pbn">
query
</code>
</a>
to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-authorization">
role
</a>
on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</p>
<p class="leafygreen-ui-1kp3ins">
If MongoDB acquires a group whose DN
<strong>
exactly
</strong>
matches the name of an
existing role, MongoDB grants the authenticated user roles and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-privileges">
privileges
</a>
associated with that role. If MongoDB
cannot map any of the returned groups to a role, MongoDB grants no
privileges to the user.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/#std-label-security-ldap">
LDAP
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/kerberos/#std-label-security-kerberos">
kerberos
</a>
authentication normally require creating users in the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database. If you also use LDAP for authorization, you do
<em>
not
</em>
need to
create users in the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database. You only need to create the
appropriate roles in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database. Users still authenticate
against the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database.
</p>
</div>
</div>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If you are using LDAP for authorization and your LDAP group DNs
contain
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://tools.ietf.org/html/rfc4514" rel="noopener noreferrer">
<span>
RFC4514
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
escaped
sequences, the roles you create in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database must also
be escaped following RFC4514.
</p>
</div>
</div>
<div class="leafygreen-ui-vg7qwp" role="note">
<h2 class="leafygreen-ui-2xicti">
Example
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
A database has the following roles configured on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n role: \"CN=dba,CN=Users,DC=example,DC=com\",\n privileges: [],\n roles: [ \"dbAdminAnyDatabase\", \"clusterAdmin\" ]\n}\n{\n role: \"CN=analytics,CN=Users,DC=example,DC=com\"\n privileges: [],\n roles: [\n { role : \"read\", db : \"web_statistics\" },\n { role : \"read\", db : \"user_statistics\" }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"CN=dba,CN=Users,DC=example,DC=com"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>: <!-- -->[<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[ <span class="lg-highlight-string">"dbAdminAnyDatabase"</span>, <span class="lg-highlight-string">"clusterAdmin"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"CN=analytics,CN=Users,DC=example,DC=com"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>: <!-- -->[<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->role <!-- -->: <span class="lg-highlight-string">"read"</span>, <!-- -->db <!-- -->: <span class="lg-highlight-string">"web_statistics"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->role <!-- -->: <span class="lg-highlight-string">"read"</span>, <!-- -->db <!-- -->: <span class="lg-highlight-string">"user_statistics"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
After authenticating a user
<code class="leafygreen-ui-1l06pbn">
[email protected]
</code>
against the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database, the MongoDB server performs a query derived from
the configured
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.ldap.authz.queryTemplate">
<code class="leafygreen-ui-1l06pbn">
query template
</code>
</a>
to retrieve the groups which include
the authenticated user as a member. In this example, the MongoDB server
retrieves the following group DNs for the user:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"dn:CN=dba,CN=Users,dc=example,dc=com\ndn:CN=admin,CN=Users,dc=example,dc=com"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">dn:CN=dba,CN=Users,dc=example,dc=com</td></tr><tr class=""><td class="leafygreen-ui-7razhx">dn:CN=admin,CN=Users,dc=example,dc=com</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
MongoDB maps these group DNs to roles on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database. The first
group DN matches the first role, and MongoDB grants the authenticated user
its roles and privileges. The second group DN does not match to any role on
the server, so MongoDB grants no additional permissions.
</p>
<p class="leafygreen-ui-1kp3ins">
A new user
<code class="leafygreen-ui-1l06pbn">
[email protected]
</code>
authenticates against the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database. The MongoDB server repeats the query process, using
the provided username in the query template. In this example, the MongoDB
server retrieves the following group DNs for the user:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"dn:cn=analytics,CN=Users,dc=example,dc=com"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">dn:cn=analytics,CN=Users,dc=example,dc=com</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
MongoDB maps these group DNs to roles on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database and grants
the authenticated user the roles and privileges of the second role.
</p>
<p class="leafygreen-ui-1kp3ins">
A new user
<code class="leafygreen-ui-1l06pbn">
[email protected]
</code>
authenticates against the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database. The MongoDB server repeats the query process, using
the provided username in the query template. In this example, the MongoDB
server retrieves the following group DNs for the user:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"dn:cn=guest,CN=Users,dc=example,dc=com"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">dn:cn=guest,CN=Users,dc=example,dc=com</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
MongoDB maps the group to a role on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database and, because no
matching roles exist, grants the user no additional permissions.
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.createRole/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.createRole()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.createrole--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="db.createrole--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#required-access" target="_self">
<span>
Required Access
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.createRole(role, writeConcern)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.createRole" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-db.createRole">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Creates a role in a database. You can specify privileges for the
role by explicitly listing the privileges or by having the role
inherit privileges from other roles or both. The role applies to
the database on which you run the method.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for database commands or language-specific drivers,
such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For the database command, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createRole/#mongodb-dbcommand-dbcmd.createRole">
<code class="leafygreen-ui-1l06pbn">
createRole
</code>
</a>
command
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.createRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.createRole()
</code>
</span>
</a>
method accepts the following arguments:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
role
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A document containing the name of the role and the role definition.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
writeConcern
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. The level of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
write concern
</a>
for the
operation. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-wc-specs">
Write Concern Specification
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
role
</code>
document has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n role: \"<name>\",\n privileges: [\n { resource: { <resource> }, actions: [ \"<action>\", ... ] },\n ...\n ],\n roles: [\n { role: \"<role>\", db: \"<database>\" } | \"<role>\",\n ...\n ],\n authenticationRestrictions: [\n {\n clientSource: [\"<ip>\" | \"<cidr>\", ...],\n serverAddress: [\"<ip>\" | \"<cidr>\", ...]\n },\n ...\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"<name>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <<!-- -->resource<!-- -->> <!-- -->}<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"<action>"</span>, <!-- -->...<!-- --> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"<role>"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"<database>"</span> } <!-- -->| <span class="lg-highlight-string">"<role>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">authenticationRestrictions</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">clientSource</span>: <!-- -->[<span class="lg-highlight-string">"<IP>"</span> <!-- -->| <span class="lg-highlight-string">"<CIDR range>"</span>, <!-- -->...<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">serverAddress</span>: <!-- -->[<span class="lg-highlight-string">"<IP>"</span> <!-- -->| <span class="lg-highlight-string">"<CIDR range>"</span>, <!-- -->...<!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
role
</code>
document has the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
role
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The name of the new role.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
privileges
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The privileges to grant the role. A privilege consists of a resource
and permitted actions. For the syntax of a privilege, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-roles-collection/#mongodb-data-admin.system.roles.privileges">
<code class="leafygreen-ui-1l06pbn">
privileges
</code>
</a>
array.
</p>
<p class="leafygreen-ui-1kp3ins">
You must include the
<code class="leafygreen-ui-1l06pbn">
privileges
</code>
field. Use an
empty array to specify
<em>
no
</em>
privileges.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
roles
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
An array of roles from which this role inherits privileges.
</p>
<p class="leafygreen-ui-1kp3ins">
You must include the
<code class="leafygreen-ui-1l06pbn">
roles
</code>
field. Use an empty array to specify
<em>
no
</em>
roles to inherit from.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
authenticationRestrictions
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional.
</p>
<p class="leafygreen-ui-1kp3ins">
The authentication restrictions the server enforces on the role.
Specifies a list of IP addresses and
<span class="leafygreen-ui-vm4wms" tabindex="0">
CIDR
</span>
ranges users granted this
role are allowed to connect to and/or which they can connect from.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Roles
<a class="headerlink leafygreen-ui-1kepx47" href="#roles" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="roles">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In the
<code class="leafygreen-ui-1l06pbn">
roles
</code>
field, you can specify both
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#std-label-built-in-roles">
built-in roles
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-user-defined-roles/#std-label-user-defined-roles">
user-defined
roles
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To specify a role that exists in the same database where
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.createRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.createRole()
</code>
</span>
</a>
runs, you can either specify the role with the name of
the role:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"readWrite\"","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">"readWrite"</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Or you can specify the role with a document, as in:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ role: \"<role>\", db: \"<database>\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"<role>"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"<database>"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To specify a role that exists in a different database, specify the role
with a document.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Authentication Restrictions
<a class="headerlink leafygreen-ui-1kepx47" href="#authentication-restrictions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="authentication-restrictions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
authenticationRestrictions
</code>
document can contain
<em>
only
</em>
the
following fields. The server throws an error if the
<code class="leafygreen-ui-1l06pbn">
authenticationRestrictions
</code>
document contains an unrecognized field:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:30%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Value
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
clientSource
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Array of IP addresses and/or
<span class="leafygreen-ui-vm4wms" tabindex="0">
CIDR
</span>
ranges
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
If present, when authenticating a user, the server verifies
that the client's IP address is either in the given list or
belongs to a CIDR range in the list. If the client's IP address
is not present, the server does not authenticate the user.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
serverAddress
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Array of IP addresses and/or
<span class="leafygreen-ui-vm4wms" tabindex="0">
CIDR
</span>
ranges
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A list of IP addresses or CIDR ranges to which the client can
connect. If present, the server will verify that the client's
connection was accepted via an IP address in the given list. If
the connection was accepted via an unrecognized IP address, the
server does not authenticate the user.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If a user inherits multiple roles with incompatible authentication
restrictions, that user becomes unusable.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, if a user inherits one role in which the
<code class="leafygreen-ui-1l06pbn">
clientSource
</code>
field is
<code class="leafygreen-ui-1l06pbn">
["198.51.100.0"]
</code>
and another role in
which the
<code class="leafygreen-ui-1l06pbn">
clientSource
</code>
field is
<code class="leafygreen-ui-1l06pbn">
["203.0.113.0"]
</code>
the server is
unable to authenticate the user.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on authentication in MongoDB, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authentication/#std-label-authentication">
Authentication on Self-Managed Deployments
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Replica set
<a class="headerlink leafygreen-ui-1kepx47" href="#replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If run on a replica set,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.createRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.createRole()
</code>
</span>
</a>
is executed using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern by default.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Scope
<a class="headerlink leafygreen-ui-1kepx47" href="#scope" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="scope">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Except for roles created in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database, a role can only
include privileges that apply to its database and can only inherit from
other roles in its database.
</p>
<p class="leafygreen-ui-1kp3ins">
A role created in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database can include privileges that
apply to the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database, other databases or to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-cluster">
cluster
</a>
resource, and can inherit from roles
in other databases as well as the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.createRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.createRole()
</code>
</span>
</a>
method returns a
<em>
duplicate role
</em>
error
if the role already exists in the database.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Required Access
<a class="headerlink leafygreen-ui-1kepx47" href="#required-access" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="required-access">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To create a role in a database, you must have:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-createRole">
<code class="leafygreen-ui-1l06pbn">
createRole
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
action
</a>
on
that
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-specific-db">
database resource
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-grantRole">
<code class="leafygreen-ui-1l06pbn">
grantRole
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
action
</a>
on
that database to specify privileges for the new role as well as to
specify roles to inherit from.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Built-in roles
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-userAdmin">
<code class="leafygreen-ui-1l06pbn">
userAdmin
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-userAdminAnyDatabase">
<code class="leafygreen-ui-1l06pbn">
userAdminAnyDatabase
</code>
</a>
provide
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-createRole">
<code class="leafygreen-ui-1l06pbn">
createRole
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-grantRole">
<code class="leafygreen-ui-1l06pbn">
grantRole
</code>
</a>
actions on their respective
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/">
resources
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To create a role with
<code class="leafygreen-ui-1l06pbn">
authenticationRestrictions
</code>
specified, you
must have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-setAuthenticationRestriction">
<code class="leafygreen-ui-1l06pbn">
setAuthenticationRestriction
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
action
</a>
on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-specific-db">
database resource
</a>
which the role is
created.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.createRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.createRole()
</code>
</span>
</a>
method creates the
<code class="leafygreen-ui-1l06pbn">
myClusterwideAdmin
</code>
role on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use admin\ndb.createRole(\n {\n role: \"myClusterwideAdmin\",\n privileges: [\n { resource: { cluster: true }, actions: [ \"addShard\" ] },\n { resource: { db: \"config\", collection: \"\" }, actions: [ \"find\", \"update\", \"insert\", \"remove\" ] },\n { resource: { db: \"users\", collection: \"usersCollection\" }, actions: [ \"update\", \"insert\", \"remove\" ] },\n { resource: { db: \"\", collection: \"\" }, actions: [ \"find\" ] }\n ],\n roles: [\n { role: \"read\", db: \"admin\" }\n ]\n },\n { w: \"majority\" , wtimeout: 5000 }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use admin</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createRole</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"myClusterwideAdmin"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">privileges</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">cluster</span>: <span class="lg-highlight-literal">true</span> }<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"addShard"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"config"</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">""</span> }<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span>, <span class="lg-highlight-string">"update"</span>, <span class="lg-highlight-string">"insert"</span>, <span class="lg-highlight-string">"remove"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"users"</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">"usersCollection"</span> }<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"update"</span>, <span class="lg-highlight-string">"insert"</span>, <span class="lg-highlight-string">"remove"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">""</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">""</span> }<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"read"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"admin"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-string">"majority"</span> <!-- -->, <span class="lg-highlight-attr">wtimeout</span>: <span class="lg-highlight-number">5000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/optimize-query-performance-with-indexes-and-projections/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Optimize Query Performance
<a class="headerlink leafygreen-ui-1kepx47" href="#optimize-query-performance" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="optimize-query-performance">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#create-indexes-to-support-queries" target="_self">
<span>
Create Indexes to Support Queries
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#limit-the-number-of-query-results-to-reduce-network-demand" target="_self">
<span>
Limit the Number of Query Results to Reduce Network Demand
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#use-projections-to-return-only-necessary-data" target="_self">
<span>
Use Projections to Return Only Necessary Data
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#use--hint-to-select-a-particular-index" target="_self">
<span>
Use
<code class="css-1kekw28 e1wawog0">
$hint
</code>
to Select a Particular Index
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#use-the-increment-operator-to-perform-operations-server-side" target="_self">
<span>
Use the Increment Operator to Perform Operations Server-Side
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Create Indexes to Support Queries
<a class="headerlink leafygreen-ui-1kepx47" href="#create-indexes-to-support-queries" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-indexes-to-support-queries">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For commonly issued queries, create
<a class="leafygreen-ui-tqgtui" href="/docs/manual/indexes/#std-label-indexes">
indexes
</a>
. If a
query searches multiple fields, create a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-index-type-compound">
compound index
</a>
. Scanning an index is much faster than scanning a
collection. The indexes structures are smaller than the documents
reference, and store references in order.
</p>
<div class="leafygreen-ui-vg7qwp" role="note">
<h2 class="leafygreen-ui-2xicti">
Example
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If you have a
<code class="leafygreen-ui-1l06pbn">
posts
</code>
collection containing blog posts,
and if you regularly issue a query that sorts on the
<code class="leafygreen-ui-1l06pbn">
author_name
</code>
field, then you can optimize the query by creating an index on the
<code class="leafygreen-ui-1l06pbn">
author_name
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.posts.createIndex( { author_name : 1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">posts</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <!-- -->author_name <!-- -->: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Indexes also improve efficiency on queries that routinely sort on a
given field.
</p>
<div class="leafygreen-ui-vg7qwp" role="note">
<h2 class="leafygreen-ui-2xicti">
Example
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If you regularly issue a query that sorts on the
<code class="leafygreen-ui-1l06pbn">
timestamp
</code>
field, then you can optimize the query by creating an
index on the
<code class="leafygreen-ui-1l06pbn">
timestamp
</code>
field:
</p>
<p class="leafygreen-ui-1kp3ins">
Creating this index:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.posts.createIndex( { timestamp : 1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">posts</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <!-- -->timestamp <!-- -->: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Optimizes this query:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.posts.find().sort( { timestamp : -1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">posts</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <!-- -->timestamp <!-- -->: <!-- -->-<span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Because MongoDB can read indexes in both ascending and descending
order, the direction of a single-key index does not matter.
</p>
<p class="leafygreen-ui-1kp3ins">
Indexes support queries, update operations, and some phases of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline-optimization/#std-label-aggregation-pipeline-optimization-indexes-and-filters">
aggregation pipeline
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Index keys that are of the
<code class="leafygreen-ui-1l06pbn">
BinData
</code>
type are more efficiently stored
in the index if:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the binary subtype value is in the range of 0-7 or 128-135, and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12,
14, 16, 20, 24, or 32.
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Limit the Number of Query Results to Reduce Network Demand
<a class="headerlink leafygreen-ui-1kepx47" href="#limit-the-number-of-query-results-to-reduce-network-demand" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="limit-the-number-of-query-results-to-reduce-network-demand">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-cursor">
cursors
</a>
return results in groups of multiple
documents. If you know the number of results you want, you can reduce
the demand on network resources by issuing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.limit/#mongodb-method-cursor.limit">
<code class="leafygreen-ui-1l06pbn">
limit()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
This is typically used in conjunction with sort operations. For example,
if you need only 10 results from your query to the
<code class="leafygreen-ui-1l06pbn">
posts
</code>
collection, you would issue the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.posts.find().sort( { timestamp : -1 } ).limit(10)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">posts</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <!-- -->timestamp <!-- -->: <!-- -->-<span class="lg-highlight-number">1</span> } ).<span class="lg-highlight-title lg-highlight-function">limit</span>(<span class="lg-highlight-number">10</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on limiting results, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.limit/#mongodb-method-cursor.limit">
<code class="leafygreen-ui-1l06pbn">
limit()
</code>
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Use Projections to Return Only Necessary Data
<a class="headerlink leafygreen-ui-1kepx47" href="#use-projections-to-return-only-necessary-data" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="use-projections-to-return-only-necessary-data">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When you need only a subset of fields from documents, you can achieve better
performance by returning only the fields you need:
</p>
<p class="leafygreen-ui-1kp3ins">
For example, if in your query to the
<code class="leafygreen-ui-1l06pbn">
posts
</code>
collection, you need only
the
<code class="leafygreen-ui-1l06pbn">
timestamp
</code>
,
<code class="leafygreen-ui-1l06pbn">
title
</code>
,
<code class="leafygreen-ui-1l06pbn">
author
</code>
, and
<code class="leafygreen-ui-1l06pbn">
abstract
</code>
fields, you
would issue the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.posts.find( {}, { timestamp : 1 , title : 1 , author : 1 , abstract : 1} ).sort( { timestamp : -1 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">posts</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{<!-- -->}<!-- -->, <!-- -->{ <!-- -->timestamp <!-- -->: <span class="lg-highlight-number">1</span> <!-- -->, <!-- -->title <!-- -->: <span class="lg-highlight-number">1</span> <!-- -->, <!-- -->author <!-- -->: <span class="lg-highlight-number">1</span> <!-- -->, <!-- -->abstract <!-- -->: <span class="lg-highlight-number">1</span>} ).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <!-- -->timestamp <!-- -->: <!-- -->-<span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on using projections, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/project-fields-from-query-results/#std-label-read-operations-projection">
Project Fields to Return from Query
<!-- -->
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Use
<code class="leafygreen-ui-1l06pbn">
$hint
</code>
to Select a Particular Index
<a class="headerlink leafygreen-ui-1kepx47" href="#use--hint-to-select-a-particular-index" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="use--hint-to-select-a-particular-index">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
In most cases the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-read-operations-query-optimization">
query optimizer
</a>
selects the optimal index for a
specific operation; however, you can force MongoDB to use a specific
index using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hint/#mongodb-method-cursor.hint">
<code class="leafygreen-ui-1l06pbn">
hint()
</code>
</a>
method. Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hint/#mongodb-method-cursor.hint">
<code class="leafygreen-ui-1l06pbn">
hint()
</code>
</a>
to support performance testing, or on
some queries where you must select a field or field included in
several indexes.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Use the Increment Operator to Perform Operations Server-Side
<a class="headerlink leafygreen-ui-1kepx47" href="#use-the-increment-operator-to-perform-operations-server-side" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="use-the-increment-operator-to-perform-operations-server-side">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use MongoDB's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/inc/#mongodb-update-up.-inc">
<code class="leafygreen-ui-1l06pbn">
$inc
</code>
</a>
operator to increment or decrement
values in documents. The operator increments the value of the field on
the server side, as an alternative to selecting a document, making
simple modifications in the client and then writing the entire
document to the server. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/inc/#mongodb-update-up.-inc">
<code class="leafygreen-ui-1l06pbn">
$inc
</code>
</a>
operator can also help
avoid race conditions, which would result when two application
instances queried for a document, manually incremented a field, and
saved the entire document back at the same time.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.revokeRolesFromRole/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.revokeRolesFromRole()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.revokerolesfromrole--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="db.revokerolesfromrole--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#required-access" target="_self">
<span>
Required Access
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.revokeRolesFromRole(rolename, roles, writeConcern)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.revokeRolesFromRole" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-db.revokeRolesFromRole">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Removes the specified inherited roles from a role.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for database commands or language-specific drivers,
such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For the database command, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/revokeRolesFromRole/#mongodb-dbcommand-dbcmd.revokeRolesFromRole">
<code class="leafygreen-ui-1l06pbn">
revokeRolesFromRole
</code>
</a>
command
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.revokeRolesFromRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.revokeRolesFromRole()
</code>
</span>
</a>
method uses the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.revokeRolesFromRole( \"<rolename>\", [ <roles> ], { <writeconcern> } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">revokeRolesFromRole</span>( <span class="lg-highlight-string">"<rolename>"</span>, <!-- -->[ <<!-- -->roles<!-- -->> <!-- -->]<!-- -->, <!-- -->{ <<!-- -->writeConcern<!-- -->> <!-- -->} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.revokeRolesFromRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.revokeRolesFromRole()
</code>
</span>
</a>
method takes the following arguments:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
rolename
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The name of the role from which to revoke roles.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
roles
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The inherited roles to remove.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
writeConcern
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. The level of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
write concern
</a>
for the
operation. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-wc-specs">
Write Concern Specification
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
In the
<code class="leafygreen-ui-1l06pbn">
roles
</code>
field, you can specify both
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#std-label-built-in-roles">
built-in roles
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-user-defined-roles/#std-label-user-defined-roles">
user-defined
roles
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To specify a role that exists in the same database where
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.revokeRolesFromRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.revokeRolesFromRole()
</code>
</span>
</a>
runs, you can either specify the role with the name of
the role:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"readWrite\"","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">"readWrite"</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Or you can specify the role with a document, as in:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ role: \"<role>\", db: \"<database>\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"<role>"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"<database>"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To specify a role that exists in a different database, specify the role
with a document.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This method is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is not supported in M0, M2, and M5 clusters.
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Replica set
<a class="headerlink leafygreen-ui-1kepx47" href="#replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If run on a replica set,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.revokeRolesFromRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.revokeRolesFromRole()
</code>
</span>
</a>
is executed using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write concern by default.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Required Access
<a class="headerlink leafygreen-ui-1kepx47" href="#required-access" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="required-access">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You must have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-revokeRole">
<code class="leafygreen-ui-1l06pbn">
revokeRole
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
action
</a>
on a database to revoke a role on that database.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
purchaseAgents
</code>
role in the
<code class="leafygreen-ui-1l06pbn">
emea
</code>
database inherits privileges
from several other roles, as listed in the
<code class="leafygreen-ui-1l06pbn">
roles
</code>
array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : \"emea.purchaseAgents\",\n \"role\" : \"purchaseAgents\",\n \"db\" : \"emea\",\n \"privileges\" : [],\n \"roles\" : [\n {\n \"role\" : \"readOrdersCollection\",\n \"db\" : \"emea\"\n },\n {\n \"role\" : \"readAccountsCollection\",\n \"db\" : \"emea\"\n },\n {\n \"role\" : \"writeOrdersCollection\",\n \"db\" : \"emea\"\n }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"emea.purchaseAgents"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"purchaseAgents"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"emea"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"privileges"</span> <!-- -->: <!-- -->[<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"roles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"readOrdersCollection"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"emea"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"readAccountsCollection"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"emea"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"writeOrdersCollection"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"emea"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.revokeRolesFromRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.revokeRolesFromRole()
</code>
</span>
</a>
operation on the
<code class="leafygreen-ui-1l06pbn">
emea
</code>
database removes two roles from the
<code class="leafygreen-ui-1l06pbn">
purchaseAgents
</code>
role:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use emea\ndb.revokeRolesFromRole( \"purchaseAgents\",\n [\n \"writeOrdersCollection\",\n \"readOrdersCollection\"\n ],\n { w: \"majority\" , wtimeout: 5000 }\n )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use emea</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">revokeRolesFromRole</span>( <span class="lg-highlight-string">"purchaseAgents"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeOrdersCollection"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"readOrdersCollection"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-string">"majority"</span> <!-- -->, <span class="lg-highlight-attr">wtimeout</span>: <span class="lg-highlight-number">5000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
purchaseAgents
</code>
role now contains just one role:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : \"emea.purchaseAgents\",\n \"role\" : \"purchaseAgents\",\n \"db\" : \"emea\",\n \"privileges\" : [],\n \"roles\" : [\n {\n \"role\" : \"readAccountsCollection\",\n \"db\" : \"emea\"\n }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"emea.purchaseAgents"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"purchaseAgents"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"emea"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"privileges"</span> <!-- -->: <!-- -->[<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"roles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"readAccountsCollection"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"emea"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/link-text-indexes/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Perform a Text Search (Self-Managed Deployments)
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-a-text-search--self-managed-deployments-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="perform-a-text-search--self-managed-deployments-">
</div>
</a>
</h1>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page describes text query capabilities for self-managed
(non-Atlas) deployments. For data hosted on MongoDB Atlas, MongoDB
offers an improved full-text query solution,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
<span>
Atlas Search
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To run text search queries on self-managed deployments, you must have a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-text/#std-label-index-feature-text">
text index
</a>
on your collection. MongoDB
provides text indexes to support text search queries on string content.
Text indexes can include any field whose value is a string or an array
of string elements. A collection can only have
<strong>
one
</strong>
text search
index, but that index can cover multiple fields.
</p>
<p class="leafygreen-ui-1kp3ins">
See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-text/#std-label-index-type-text">
Text Indexes on Self-Managed Deployments
</a>
section for a full reference on text
indexes, including behavior, tokenization, and properties.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-text-index-eg">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This example demonstrates how to build a text index and use it to find
coffee shops, given only text fields.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create a Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-collection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-a-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
stores
</code>
with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stores.insertMany(\n [\n { _id: 1, name: \"Java Hut\", description: \"Coffee and cakes\" },\n { _id: 2, name: \"Burger Buns\", description: \"Gourmet hamburgers\" },\n { _id: 3, name: \"Coffee Shop\", description: \"Just coffee\" },\n { _id: 4, name: \"Clothes Clothes Clothes\", description: \"Discount clothing\" },\n { _id: 5, name: \"Java Shopping\", description: \"Indonesian goods\" }\n ]\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stores</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Java Hut"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"Coffee and cakes"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Burger Buns"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"Gourmet hamburgers"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Coffee Shop"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"Just coffee"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Clothes Clothes Clothes"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"Discount clothing"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Java Shopping"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"Indonesian goods"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create a Text Index
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-text-index" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-a-text-index">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Run the following in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
to allow text search over
the
<code class="leafygreen-ui-1l06pbn">
name
</code>
and
<code class="leafygreen-ui-1l06pbn">
description
</code>
fields:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stores.createIndex( { name: \"text\", description: \"text\" } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stores</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"text"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"text"</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Search for an Exact Phrase
<a class="headerlink leafygreen-ui-1kepx47" href="#search-for-an-exact-phrase" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="search-for-an-exact-phrase">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can also search for exact phrases by wrapping them in double-quotes.
If the
<code class="leafygreen-ui-1l06pbn">
$search
</code>
string includes a phrase and individual terms, text
search will only match documents that include the phrase.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following will find all documents containing
"coffee shop":
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stores.find( { $text: { $search: \"\\\"coffee shop\\\"\" } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stores</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">$text</span>: <!-- -->{ <span class="lg-highlight-attr">$search</span>: <span class="lg-highlight-string">"</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">coffee shop</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">"</span> } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/text/#std-label-text-operator-phrases">
Phrases
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Exclude a Term
<a class="headerlink leafygreen-ui-1kepx47" href="#exclude-a-term" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="exclude-a-term">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To exclude a word, you can prepend a "
<code class="leafygreen-ui-1l06pbn">
-
</code>
" character. For example, to
find all stores containing "java" or "shop" but not "coffee", use the
following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stores.find( { $text: { $search: \"java shop -coffee\" } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stores</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">$text</span>: <!-- -->{ <span class="lg-highlight-attr">$search</span>: <span class="lg-highlight-string">"java shop -coffee"</span> } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sort the Results
<a class="headerlink leafygreen-ui-1kepx47" href="#sort-the-results" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sort-the-results">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB returns its results in unsorted order by default. However,
<code class="leafygreen-ui-1l06pbn">
$text
</code>
queries compute a relevance score for each document
that specifies how well a document matches the query.
</p>
<p class="leafygreen-ui-1kp3ins">
To sort the results in order of relevance score, you must explicitly
project the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/meta/#mongodb-expression-exp.-meta">
<code class="leafygreen-ui-1l06pbn">
$meta
</code>
</a>
<code class="leafygreen-ui-1l06pbn">
textScore
</code>
field and sort on it:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stores.find(\n { $text: { $search: \"java coffee shop\" } },\n { score: { $meta: \"textScore\" } }\n).sort( { score: { $meta: \"textScore\" } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stores</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$text</span>: <!-- -->{ <span class="lg-highlight-attr">$search</span>: <span class="lg-highlight-string">"java coffee shop"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">score</span>: <!-- -->{ <span class="lg-highlight-attr">$meta</span>: <span class="lg-highlight-string">"textScore"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">score</span>: <!-- -->{ <span class="lg-highlight-attr">$meta</span>: <span class="lg-highlight-string">"textScore"</span> } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$text
</code>
is also available in the aggregation pipeline.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/query/all/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$all
<a class="headerlink leafygreen-ui-1kepx47" href="#-all" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-all">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#additional-examples" target="_self">
<span>
Additional Examples
</span>
</a>
</li>
</ul>
</div>
<dl class="query">
<dt>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-query-op.-all" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-query-op.-all">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
operator selects the documents where the value of
a field is an array that contains all the specified elements.
</p>
</dd>
</dl>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can use
<code class="leafygreen-ui-1l06pbn">
$all
</code>
for deployments hosted in the following
environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To specify an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
expression, use the following prototype:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ <field>: { $all: [ <value1> , <value2> ... ] } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <<!-- -->field<!-- -->><!-- -->: <!-- -->{ <span class="lg-highlight-attr">$all</span>: <!-- -->[ <<!-- -->value1<!-- -->> <!-- -->,<!-- --> <!-- --><<!-- -->value2<!-- -->> <!-- -->...<!-- --> ] } }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-all-operator-behavior">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Equivalent to
<code class="leafygreen-ui-1l06pbn">
$and
</code>
Operation
<a class="headerlink leafygreen-ui-1kepx47" href="#equivalent-to--and-operation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="equivalent-to--and-operation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
is equivalent to an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/and/#mongodb-query-op.-and">
<code class="leafygreen-ui-1l06pbn">
$and
</code>
</a>
operation of the
specified values; i.e. the following statement:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ tags: { $all: [ \"ssl\" , \"security\" ] } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">tags</span>: <!-- -->{ <span class="lg-highlight-attr">$all</span>: <!-- -->[ <span class="lg-highlight-string">"ssl"</span> <!-- -->, <span class="lg-highlight-string">"security"</span> ] } }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
is equivalent to:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $and: [ { tags: \"ssl\" }, { tags: \"security\" } ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$and</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">tags</span>: <span class="lg-highlight-string">"ssl"</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">tags</span>: <span class="lg-highlight-string">"security"</span> } ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Nested Array
<a class="headerlink leafygreen-ui-1kepx47" href="#nested-array" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="nested-array">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When passed an array of a nested array (e.g.
<code class="leafygreen-ui-1l06pbn">
[ [ "A" ] ]
</code>
),
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
matches documents where the field contains the
nested array as an element (e.g.
<code class="leafygreen-ui-1l06pbn">
field: [ [ "A" ], ... ]
</code>
),
<em>
or
</em>
the
field equals the nested array (e.g.
<code class="leafygreen-ui-1l06pbn">
field: [ "A" ]
</code>
).
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider the following query
<a class="footnote-reference header-buffer" href="#footnote-illustrative" id="ref-illustrative-id1">
[
<!-- -->
1
<!-- -->
]
</a>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.articles.find( { tags: { $all: [ [ \"ssl\", \"security\" ] ] } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">articles</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">tags</span>: <!-- -->{ <span class="lg-highlight-attr">$all</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"ssl"</span>, <span class="lg-highlight-string">"security"</span> ] ] } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The query is equivalent to:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.articles.find( { $and: [ { tags: [ \"ssl\", \"security\" ] } ] } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">articles</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">$and</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"ssl"</span>, <span class="lg-highlight-string">"security"</span> ] } ] } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
which is equivalent to:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.articles.find( { tags: [ \"ssl\", \"security\" ] } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">articles</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"ssl"</span>, <span class="lg-highlight-string">"security"</span> ] } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
As such, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
expression matches documents where the
<code class="leafygreen-ui-1l06pbn">
tags
</code>
field is an array that contains the nested array
<code class="leafygreen-ui-1l06pbn">
[ "ssl",
"security" ]
</code>
or is an array that equals the nested array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"tags: [ [ \"ssl\", \"security\" ], ... ]\ntags: [ \"ssl\", \"security\" ]","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">tags</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"ssl"</span>, <span class="lg-highlight-string">"security"</span> ]<!-- -->, <!-- -->...<!-- --> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"ssl"</span>, <span class="lg-highlight-string">"security"</span> ]</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-illustrative" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-illustrative-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
expression with a
<em>
single
</em>
element is for
illustrative purposes since the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
expression is
unnecessary if matching only a single element. Instead, when
matching a single element, a "contains" expression (i.e.
<code class="leafygreen-ui-1l06pbn">
arrayField: element
</code>
) is more suitable.
</td>
</tr>
</tbody>
</table>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following examples use the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection that contains
the documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: ObjectId(\"5234cc89687ea597eabee675\"),\n code: \"xyz\",\n tags: [ \"school\", \"book\", \"bag\", \"headphone\", \"appliance\" ],\n qty: [\n { size: \"S\", num: 10, color: \"blue\" },\n { size: \"M\", num: 45, color: \"blue\" },\n { size: \"L\", num: 100, color: \"green\" }\n ]\n}\n\n{\n _id: ObjectId(\"5234cc8a687ea597eabee676\"),\n code: \"abc\",\n tags: [ \"appliance\", \"school\", \"book\" ],\n qty: [\n { size: \"6\", num: 100, color: \"green\" },\n { size: \"6\", num: 50, color: \"blue\" },\n { size: \"8\", num: 100, color: \"brown\" }\n ]\n}\n\n{\n _id: ObjectId(\"5234ccb7687ea597eabee677\"),\n code: \"efg\",\n tags: [ \"school\", \"book\" ],\n qty: [\n { size: \"S\", num: 10, color: \"blue\" },\n { size: \"M\", num: 100, color: \"blue\" },\n { size: \"L\", num: 100, color: \"green\" }\n ]\n}\n\n{\n _id: ObjectId(\"52350353b2eff1353b349de9\"),\n code: \"ijk\",\n tags: [ \"electronics\", \"school\" ],\n qty: [\n { size: \"M\", num: 100, color: \"green\" }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5234cc89687ea597eabee675"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-string">"xyz"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"book"</span>, <span class="lg-highlight-string">"bag"</span>, <span class="lg-highlight-string">"headphone"</span>, <span class="lg-highlight-string">"appliance"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"S"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">45</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"L"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"green"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5234cc8a687ea597eabee676"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-string">"abc"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"appliance"</span>, <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"book"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"6"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"green"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"6"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"8"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"brown"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5234ccb7687ea597eabee677"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-string">"efg"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"book"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"S"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"L"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"green"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"52350353b2eff1353b349de9"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-string">"ijk"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"electronics"</span>, <span class="lg-highlight-string">"school"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"green"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-match-values-with-all">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use
<code class="leafygreen-ui-1l06pbn">
$all
</code>
to Match Values
<a class="headerlink leafygreen-ui-1kepx47" href="#use--all-to-match-values" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="use--all-to-match-values">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
operator to query the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection for documents where the value of the
<code class="leafygreen-ui-1l06pbn">
tags
</code>
field is an array whose elements include
<code class="leafygreen-ui-1l06pbn">
appliance
</code>
,
<code class="leafygreen-ui-1l06pbn">
school
</code>
, and
<code class="leafygreen-ui-1l06pbn">
book
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.find( { tags: { $all: [ \"appliance\", \"school\", \"book\" ] } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">tags</span>: <!-- -->{ <span class="lg-highlight-attr">$all</span>: <!-- -->[ <span class="lg-highlight-string">"appliance"</span>, <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"book"</span> ] } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The above query returns the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: ObjectId(\"5234cc89687ea597eabee675\"),\n code: \"xyz\",\n tags: [ \"school\", \"book\", \"bag\", \"headphone\", \"appliance\" ],\n qty: [\n { size: \"S\", num: 10, color: \"blue\" },\n { size: \"M\", num: 45, color: \"blue\" },\n { size: \"L\", num: 100, color: \"green\" }\n ]\n}\n\n{\n _id: ObjectId(\"5234cc8a687ea597eabee676\"),\n code: \"abc\",\n tags: [ \"appliance\", \"school\", \"book\" ],\n qty: [\n { size: \"6\", num: 100, color: \"green\" },\n { size: \"6\", num: 50, color: \"blue\" },\n { size: \"8\", num: 100, color: \"brown\" }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5234cc89687ea597eabee675"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-string">"xyz"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"book"</span>, <span class="lg-highlight-string">"bag"</span>, <span class="lg-highlight-string">"headphone"</span>, <span class="lg-highlight-string">"appliance"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"S"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">45</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"L"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"green"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5234cc8a687ea597eabee676"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">code</span>: <span class="lg-highlight-string">"abc"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</span>: <!-- -->[ <span class="lg-highlight-string">"appliance"</span>, <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"book"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"6"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"green"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"6"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"8"</span>, <span class="lg-highlight-attr">num</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"brown"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-all-with-elemMatch">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use
<code class="leafygreen-ui-1l06pbn">
$all
</code>
with
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#use--all-with--elemmatch" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="use--all-with--elemmatch">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the field contains an array of documents, you can use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/elemMatch/#mongodb-query-op.-elemMatch">
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</a>
operator.
</p>
<p class="leafygreen-ui-1kp3ins">
The following operation queries the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection for
documents where the value of the
<code class="leafygreen-ui-1l06pbn">
qty
</code>
field is an array whose
elements match the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/elemMatch/#mongodb-query-op.-elemMatch">
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</a>
criteria:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.find( {\n qty: { $all: [\n { \"$elemMatch\" : { size: \"M\", num: { $gt: 50} } },\n { \"$elemMatch\" : { num : 100, color: \"green\" } }\n ] }\n } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">qty</span>: <!-- -->{ <span class="lg-highlight-attr">$all</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"$elemMatch"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-attr">num</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">50</span>} } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"$elemMatch"</span> <!-- -->: <!-- -->{ <!-- -->num <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">color</span>: <span class="lg-highlight-string">"green"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The query returns the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"5234ccb7687ea597eabee677\"),\n \"code\" : \"efg\",\n \"tags\" : [ \"school\", \"book\"],\n \"qty\" : [\n { \"size\" : \"S\", \"num\" : 10, \"color\" : \"blue\" },\n { \"size\" : \"M\", \"num\" : 100, \"color\" : \"blue\" },\n { \"size\" : \"L\", \"num\" : 100, \"color\" : \"green\" }\n ]\n}\n\n{\n \"_id\" : ObjectId(\"52350353b2eff1353b349de9\"),\n \"code\" : \"ijk\",\n \"tags\" : [ \"electronics\", \"school\" ],\n \"qty\" : [\n { \"size\" : \"M\", \"num\" : 100, \"color\" : \"green\" }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5234ccb7687ea597eabee677"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"code"</span> <!-- -->: <span class="lg-highlight-string">"efg"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"school"</span>, <span class="lg-highlight-string">"book"</span>]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"qty"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"size"</span> <!-- -->: <span class="lg-highlight-string">"S"</span>, <span class="lg-highlight-string">"num"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"color"</span> <!-- -->: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"size"</span> <!-- -->: <span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-string">"num"</span> <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-string">"color"</span> <!-- -->: <span class="lg-highlight-string">"blue"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"size"</span> <!-- -->: <span class="lg-highlight-string">"L"</span>, <span class="lg-highlight-string">"num"</span> <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-string">"color"</span> <!-- -->: <span class="lg-highlight-string">"green"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"52350353b2eff1353b349de9"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"code"</span> <!-- -->: <span class="lg-highlight-string">"ijk"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"electronics"</span>, <span class="lg-highlight-string">"school"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"qty"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"size"</span> <!-- -->: <span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-string">"num"</span> <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-string">"color"</span> <!-- -->: <span class="lg-highlight-string">"green"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
operator exists to support queries on arrays. But
you may use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-all" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</span>
</a>
operator to select against a non-array
<code class="leafygreen-ui-1l06pbn">
field
</code>
, as in the following example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.find( { \"qty.num\": { $all: [ 50 ] } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-string">"qty.num"</span>: <!-- -->{ <span class="lg-highlight-attr">$all</span>: <!-- -->[ <span class="lg-highlight-number">50</span> ] } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<strong>
However
</strong>
, use the following form to express the same query:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.find( { \"qty.num\" : 50 } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-string">"qty.num"</span> <!-- -->: <span class="lg-highlight-number">50</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Both queries will select all documents in the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection where the value of the
<code class="leafygreen-ui-1l06pbn">
num
</code>
field equals
<code class="leafygreen-ui-1l06pbn">
50
</code>
.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
In most cases, MongoDB does not treat arrays as sets. This operator
provides a notable exception to this approach.
</p>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Additional Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#additional-examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="additional-examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For additional examples on querying arrays, see:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-arrays/#std-label-read-operations-arrays">
Query an Array
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-array-of-documents/#std-label-array-match-embedded-documents">
Query an Array of Embedded Documents
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For additional examples on querying, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-documents/#std-label-read-operations-query-document">
Query Documents
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/cursor.explain/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
cursor.explain()
<a class="headerlink leafygreen-ui-1kepx47" href="#cursor.explain--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="cursor.explain--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output" target="_self">
<span>
Output
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
cursor.explain(verbosity)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-cursor.explain" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-cursor.explain">
</div>
</a>
</dt>
<dd>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for a language-specific driver, such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Provides information on the query plan for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
</span>
</a>
method has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.find().explain()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">explain</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
</span>
</a>
method has the following parameter:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
verbose
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies the verbosity mode for the explain output. The mode affects
the behavior of
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
and determines the amount of
information to return. The possible modes are:
<code class="leafygreen-ui-1l06pbn">
"queryPlanner"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"executionStats"
</code>
, and
<code class="leafygreen-ui-1l06pbn">
"allPlansExecution"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Default mode is
<code class="leafygreen-ui-1l06pbn">
"queryPlanner"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For backwards compatibility with earlier versions of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</span>
</a>
, MongoDB interprets
<code class="leafygreen-ui-1l06pbn">
true
</code>
as
<code class="leafygreen-ui-1l06pbn">
"allPlansExecution"
</code>
and
<code class="leafygreen-ui-1l06pbn">
false
</code>
as
<code class="leafygreen-ui-1l06pbn">
"queryPlanner"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on the modes, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-explain-cursor-method-verbosity" target="_self">
<span>
Verbosity Modes
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
</span>
</a>
method returns a document with the
query plan and, optionally, the execution statistics.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Using
<code class="leafygreen-ui-1l06pbn">
explain
</code>
ignores all existing plan cache entries and prevents
the MongoDB query planner from creating a new plan cache entry.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-cursor-method-verbosity">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Verbosity Modes
<a class="headerlink leafygreen-ui-1kepx47" href="#verbosity-modes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="verbosity-modes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The behavior of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</span>
</a>
and the amount of
information returned depend on the
<code class="leafygreen-ui-1l06pbn">
verbosity
</code>
mode.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-cursor-method-queryPlanner">
</span>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
Mode
<a class="headerlink leafygreen-ui-1kepx47" href="#queryplanner-mode" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="queryplanner-mode">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
By default,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</span>
</a>
runs in
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
verbosity mode.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB runs the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-read-operations-query-optimization">
query optimizer
</a>
to choose
the winning plan for the operation under evaluation.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</span>
</a>
returns
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
information for the evaluated
<!-- -->
method
<!-- -->
.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-cursor-method-executionStats">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
Mode
<a class="headerlink leafygreen-ui-1kepx47" href="#executionstats-mode" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="executionstats-mode">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
MongoDB runs the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-read-operations-query-optimization">
query optimizer
</a>
to choose
the winning plan, executes the winning plan to completion, and returns
statistics describing the execution of the winning plan.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</span>
</a>
returns the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
information for the evaluated
<!-- -->
method
<!-- -->
. However,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
does not
provide query execution information for the rejected plans.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-cursor-method-allPlansExecution">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
allPlansExecution
</code>
Mode
<a class="headerlink leafygreen-ui-1kepx47" href="#allplansexecution-mode" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="allplansexecution-mode">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
MongoDB runs the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/">
query optimizer
</a>
to choose the winning plan and executes the
winning plan to completion. In
<code class="leafygreen-ui-1l06pbn">
"allPlansExecution"
</code>
mode, MongoDB
returns statistics describing the execution of the winning plan as well
as statistics for the other candidate plans captured during
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
plan
selection
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</span>
</a>
returns the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
information for the evaluated
<!-- -->
method
<!-- -->
. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
includes the
<em>
completed
</em>
query execution information for the
<em>
winning plan
</em>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If the query optimizer considered more than one plan,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
information also includes the
<em>
partial
</em>
execution information captured during the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
plan selection phase
</a>
for both the winning and rejected
candidate plans.
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#db.collection.explain--.find--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="db.collection.explain--.find--">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.explain/#mongodb-method-db.collection.explain">
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
</a>
is similar to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find().explain()
</code>
</span>
</a>
with the
following key differences:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.explain/#mongodb-method-db.collection.explain">
<code class="leafygreen-ui-1l06pbn">
db.collection.explain().find()
</code>
</a>
construct allows for the
additional chaining of query modifiers. For list of query modifiers,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.explain/#std-label-explain-method-help">
db.collection.explain().find().help()
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.explain/#mongodb-method-db.collection.explain">
<code class="leafygreen-ui-1l06pbn">
db.collection.find().explain()
</code>
</a>
returns a cursor, which
requires a call to
<code class="leafygreen-ui-1l06pbn">
.next()
</code>
, or its alias
<code class="leafygreen-ui-1l06pbn">
.finish()
</code>
, to return
the
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
results.
If run interactively in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
automatically calls
<code class="leafygreen-ui-1l06pbn">
.finish()
</code>
to return
the results. For scripts, however, you must explicitly call
<code class="leafygreen-ui-1l06pbn">
.next()
</code>
, or
<code class="leafygreen-ui-1l06pbn">
.finish()
</code>
, to return the results. For list of
cursor-related methods, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.explain/#std-label-explain-method-help">
db.collection.explain().find().help()
<!-- -->
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.explain/#mongodb-method-db.collection.explain">
<code class="leafygreen-ui-1l06pbn">
db.collection.explain()
</code>
</a>
for more information.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-explain-cursor-method-output">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Output
<a class="headerlink leafygreen-ui-1kepx47" href="#output" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="output">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</span>
</a>
operations can return information regarding:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
explainVersion
</code>
, the output format version (for example,
<code class="leafygreen-ui-1l06pbn">
"1"
</code>
).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
command
</code>
, which details the command being explained.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
queryShapeHash
</code>
, starting in MongoDB 8.0, which is a hexadecimal
string with the hash of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-shape">
query shape
</a>
. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-shapes/#std-label-query-shapes">
Query Shapes
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-explain-output-query-shape-hash">
Query Shape Hash
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryShapeHash">
<code class="leafygreen-ui-1l06pbn">
explain.queryShapeHash
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-queryPlanner">
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
</a>
, which details the plan selected by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-read-operations-query-optimization">
query optimizer
</a>
and lists the rejected
plans.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
, which details the execution of the winning
plan and the rejected plans.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-serverInfo">
<code class="leafygreen-ui-1l06pbn">
serverInfo
</code>
</a>
, which provides information on the
MongoDB instance.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
serverParameters
</code>
, which details internal parameters.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The verbosity mode (i.e.
<code class="leafygreen-ui-1l06pbn">
queryPlanner
</code>
,
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
,
<code class="leafygreen-ui-1l06pbn">
allPlansExecution
</code>
) determines whether the results include
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
and whether
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-executionStats">
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
</a>
includes data
captured during
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
plan selection
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Explain output is limited by the maximum
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-Nested-Depth-for-BSON-Documents">
Nested Depth for BSON Documents
</a>
, which is 100 levels of nesting. Explain output that exceeds
the limit is truncated.
</p>
<p class="leafygreen-ui-1kp3ins">
For details on the output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/">
Explain Results
<!-- -->
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following example runs
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.explain" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</span>
</a>
in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.explain/#std-label-explain-method-executionStats">
"executionStats"
</a>
verbosity mode
to return the query planning and execution information for the
specified
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
operation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.find(\n { quantity: { $gt: 50 }, category: \"apparel\" }\n).explain(\"executionStats\")","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">quantity</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">50</span> }<!-- -->, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"apparel"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">).<span class="lg-highlight-title lg-highlight-function">explain</span>(<span class="lg-highlight-string">"executionStats"</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2d/calculate-distances/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Convert Distance to Radians for Spherical Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-distance-to-radians-for-spherical-operators" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="convert-distance-to-radians-for-spherical-operators">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#about-this-task" target="_self">
<span>
About this Task
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#convert-miles-to-radians" target="_self">
<span>
Convert Miles to Radians
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#convert-kilometers-to-radians" target="_self">
<span>
Convert Kilometers to Radians
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
2d indexes support certain query operators that calculate distances
using spherical geometry. Spherical query operators use radians for
distance. To use spherical query operators with a 2d index, you must
convert distances to radians.
</p>
<p class="leafygreen-ui-1kp3ins">
2d indexes support the following spherical query operators:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/centerSphere/#mongodb-query-op.-centerSphere">
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/geoNear/#mongodb-pipeline-pipe.-geoNear">
<code class="leafygreen-ui-1l06pbn">
$geoNear
</code>
</a>
pipeline stage with the
<code class="leafygreen-ui-1l06pbn">
spherical: true
</code>
option
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/near/#mongodb-query-op.-near">
<code class="leafygreen-ui-1l06pbn">
$near
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/nearSphere/#mongodb-query-op.-nearSphere">
<code class="leafygreen-ui-1l06pbn">
$nearSphere
</code>
</a>
</p>
</li>
</ul>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
About this Task
<a class="headerlink leafygreen-ui-1kepx47" href="#about-this-task" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="about-this-task">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Using a 2d index for queries on spherical data can return incorrect
results or an error. For example, 2d indexes don't support spherical
queries that wrap around the poles.
</p>
<p class="leafygreen-ui-1kp3ins">
If your data is stored as longitude and latitude and you often run
queries on spherical surfaces, use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/#std-label-2dsphere-index">
2dsphere index
</a>
instead of a 2d index.
</p>
<p class="leafygreen-ui-1kp3ins">
When you specify longitude and latitude coordinates, list the
<strong>
longitude
</strong>
first, and then
<strong>
latitude
</strong>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Valid longitude values are between
<code class="leafygreen-ui-1l06pbn">
-180
</code>
and
<code class="leafygreen-ui-1l06pbn">
180
</code>
, both
inclusive.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Valid latitude values are between
<code class="leafygreen-ui-1l06pbn">
-90
</code>
and
<code class="leafygreen-ui-1l06pbn">
90
</code>
, both
inclusive.
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To convert distance to radians, divide the distance by the radius of the
sphere (for example, the Earth) in the same units as the distance
measurement.
</p>
<p class="leafygreen-ui-1kp3ins">
The equatorial radius of Earth is approximately 3,963.2 miles or 6,378.1
kilometers.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following examples use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/centerSphere/#mongodb-query-op.-centerSphere">
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
</a>
operator to
perform queries. The
<code class="leafygreen-ui-1l06pbn">
$centerSphere
</code>
operator uses radians to
calculate distance.
</p>
<p class="leafygreen-ui-1kp3ins">
Create the
<code class="leafygreen-ui-1l06pbn">
contacts
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.contacts.insertMany( [\n {\n name: \"Evander Otylia\",\n phone: \"202-555-0193\",\n address: [ 55.5, 42.3 ]\n },\n {\n name: \"Georgine Lestaw\",\n phone: \"714-555-0107\",\n address: [ -74, 44.74 ]\n }\n] )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">contacts</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Evander Otylia"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">phone</span>: <span class="lg-highlight-string">"202-555-0193"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">address</span>: <!-- -->[ <span class="lg-highlight-number">55.5</span>, <span class="lg-highlight-number">42.3</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Georgine Lestaw"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">phone</span>: <span class="lg-highlight-string">"714-555-0107"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">address</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">74</span>, <span class="lg-highlight-number">44.74</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
address
</code>
field contains
<a class="leafygreen-ui-tqgtui" href="/docs/manual/geospatial-queries/#std-label-geospatial-legacy">
legacy coordinate pairs
<!-- -->
.
</a>
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert Miles to Radians
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-miles-to-radians" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="convert-miles-to-radians">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following query returns documents where the
<code class="leafygreen-ui-1l06pbn">
address
</code>
field is
within a circle with center point
<code class="leafygreen-ui-1l06pbn">
[ -72, 44 ]
</code>
and a radius of 200
miles:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.contacts.find(\n {\n address:\n {\n $geoWithin:\n {\n $centerSphere:\n [\n [ -72, 44 ] ,\n 200 / 3963.2\n ]\n }\n }\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">contacts</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">address</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geoWithin</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$centerSphere</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <!-- -->-<span class="lg-highlight-number">72</span>, <span class="lg-highlight-number">44</span> ] <!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-number">200</span> <!-- -->/ <span class="lg-highlight-number">3963.2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId(\"647e565c6cdaf4dc323ec92d\"),\n name: 'Georgine Lestaw',\n phone: '714-555-0107',\n address: [ -74, 44.74 ]\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"647e565c6cdaf4dc323ec92d"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Georgine Lestaw'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">phone</span>: <span class="lg-highlight-string">'714-555-0107'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">address</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">74</span>, <span class="lg-highlight-number">44.74</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the preceding query, to convert 200 miles to radians, the specified
miles were divided by 3963.2.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert Kilometers to Radians
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-kilometers-to-radians" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="convert-kilometers-to-radians">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following query returns documents where the
<code class="leafygreen-ui-1l06pbn">
address
</code>
field is
within a circle with center point
<code class="leafygreen-ui-1l06pbn">
[ 55, 42 ]
</code>
and a radius of 500
kilometers:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.contacts.find(\n {\n address:\n {\n $geoWithin:\n {\n $centerSphere:\n [\n [ 55, 42 ] ,\n 500 / 6378.1\n ]\n }\n }\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">contacts</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">address</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geoWithin</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$centerSphere</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <span class="lg-highlight-number">55</span>, <span class="lg-highlight-number">42</span> ] <!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-number">500</span> <!-- -->/ <span class="lg-highlight-number">6378.1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId(\"647e565c6cdaf4dc323ec92c\"),\n name: 'Evander Otylia',\n phone: '202-555-0193',\n address: [ 55.5, 42.3 ]\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"647e565c6cdaf4dc323ec92c"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Evander Otylia'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">phone</span>: <span class="lg-highlight-string">'202-555-0193'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">address</span>: <!-- -->[ <span class="lg-highlight-number">55.5</span>, <span class="lg-highlight-number">42.3</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the preceding query, to convert 500 kilometers to radians, the
specified kilometers were divided by 6378.1.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/queryable-encryption/qe-create-encryption-schema/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Create an Encryption Schema
<a class="headerlink leafygreen-ui-1kepx47" href="#create-an-encryption-schema" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-an-encryption-schema">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#about-this-task" target="_self">
<span>
About this Task
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#before-you-begin" target="_self">
<span>
Before you Begin
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#steps" target="_self">
<span>
Steps
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#create-a-json-encryption-schema-document." target="_self">
<span>
Create a JSON encryption schema document.
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#specify-fields-to-encrypt." target="_self">
<span>
Specify fields to encrypt.
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#enable-equality-queries-on-desired-fields." target="_self">
<span>
Enable equality queries on desired fields.
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#enable-range-queries-on-desired-fields." target="_self">
<span>
Enable range queries on desired fields.
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
About this Task
<a class="headerlink leafygreen-ui-1kepx47" href="#about-this-task" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="about-this-task">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To make encrypted fields queryable, create an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-encryption-schema">
encryption schema
</a>
.
This schema defines which fields are queryable, and which
query types are permitted. For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/#std-label-qe-fundamentals-encrypt-query">
Encrypted Fields and Enabled Queries
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption supports equality and range queries. You can configure a
field for only one query type.
</p>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Before you Begin
<a class="headerlink leafygreen-ui-1kepx47" href="#before-you-begin" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="before-you-begin">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When you make encrypted fields queryable, consider performance and
security. For details on how each configuration option affects these,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/#std-label-qe-field-configuration">
Configure Encrypted Fields for Optimal Search and Storage
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-specify-fields-for-encryption">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Steps
<a class="headerlink leafygreen-ui-1kepx47" href="#steps" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="steps">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Include an encryptedFieldsObject with a nested fields array:","name":"Create a JSON encryption schema document."},{"@type":"HowToStep","text":"Add the path and bsonType strings to a document\nwithin the fields array:You can specify any field for encryption except the\n_id field.If you are using explicit encryption, add a keyId field\nwith the DEK (Data Encryption Key) ID:With Automatic Encryption, MongoDB creates encryption keys for\neach field. You configure AutoEncryptionSettings on the\nclient, then use the createEncryptedCollection helper method\nto create your collections.","name":"Specify fields to encrypt."},{"@type":"HowToStep","text":"This enables querying with the $eq, $ne, $in,\nand $nin operators.Add the queries object and set queryType to \"equality\":","name":"Enable equality queries on desired fields."},{"@type":"HowToStep","text":"This enables querying with the $lt, $lte, $gt, and $gte operators.For details on how the following options affect security and performance, see Configure Encrypted Fields for Optimal Search and Storage.Add the queries object and set queryType to \"range\":Set the following fields:FieldTypeDescriptionmin and maxSame as field bsonTypeRequired if bsonType is decimal or\ndouble. Optional but highly recommended if it is int,\nlong, or date. Defaults to the minimum and maximum\nvalues of the bsonType.When possible, specifying bounds on a query improves\nperformance. If querying values outside of these inclusive\nbounds, MongoDB returns an error.","name":"Enable range queries on desired fields."}],"name":"Steps","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create a JSON encryption schema document.
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-json-encryption-schema-document." title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-a-json-encryption-schema-document.">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Include an
<code class="leafygreen-ui-1l06pbn">
encryptedFieldsObject
</code>
with a nested
<code class="leafygreen-ui-1l06pbn">
fields
</code>
array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const encryptedFieldsObject = {\n fields: []\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> encryptedFieldsObject <!-- -->= <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">fields</span>: <!-- -->[<!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Specify fields to encrypt.
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-fields-to-encrypt." title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="specify-fields-to-encrypt.">
</div>
</a>
</h3>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Add the
<code class="leafygreen-ui-1l06pbn">
path
</code>
and
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
strings to a document
within the fields array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const encryptedFieldsObject = {\n fields: [\n {\n path: \"myDocumentField\",\n bsonType: \"int\"\n }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> encryptedFieldsObject <!-- -->= <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">fields</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"myDocumentField"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"int"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
You can specify any field for encryption except the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field.
</p>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you are using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/manual-encryption/#std-label-qe-fundamentals-manual-encryption">
explicit encryption
</a>
, add a
<code class="leafygreen-ui-1l06pbn">
keyId
</code>
field
with the
<span class="leafygreen-ui-vm4wms" tabindex="0">
DEK
</span>
ID:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n path: \"myDocumentField\",\n bsonType: \"int\",\n keyId: \"<unique>\"\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"myDocumentField"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"int"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">keyId</span>: <span class="lg-highlight-string">"<unique data encryption key>"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
With Automatic Encryption, MongoDB creates encryption keys for
each field. You configure
<code class="leafygreen-ui-1l06pbn">
AutoEncryptionSettings
</code>
on the
client, then use the
<code class="leafygreen-ui-1l06pbn">
createEncryptedCollection
</code>
helper method
to create your collections.
</p>
</div>
</div>
</li>
</ol>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Enable equality queries on desired fields.
<a class="headerlink leafygreen-ui-1kepx47" href="#enable-equality-queries-on-desired-fields." title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="enable-equality-queries-on-desired-fields.">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This enables querying with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/eq/#mongodb-query-op.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/ne/#mongodb-query-op.-ne">
<code class="leafygreen-ui-1l06pbn">
$ne
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/in/#mongodb-query-op.-in">
<code class="leafygreen-ui-1l06pbn">
$in
</code>
</a>
,
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/nin/#mongodb-query-op.-nin">
<code class="leafygreen-ui-1l06pbn">
$nin
</code>
</a>
operators.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-enable-queries">
</span>
<p class="leafygreen-ui-1kp3ins">
Add the
<code class="leafygreen-ui-1l06pbn">
queries
</code>
object and set
<code class="leafygreen-ui-1l06pbn">
queryType
</code>
to
<code class="leafygreen-ui-1l06pbn">
"equality"
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n path: \"myDocumentField\",\n bsonType: \"int\",\n queries: { queryType: \"equality\" }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"myDocumentField"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"int"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queries</span>: <!-- -->{ <span class="lg-highlight-attr">queryType</span>: <span class="lg-highlight-string">"equality"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
4
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Enable range queries on desired fields.
<a class="headerlink leafygreen-ui-1kepx47" href="#enable-range-queries-on-desired-fields." title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="enable-range-queries-on-desired-fields.">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This enables querying with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/lt/#mongodb-query-op.-lt">
<code class="leafygreen-ui-1l06pbn">
$lt
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/lte/#mongodb-query-op.-lte">
<code class="leafygreen-ui-1l06pbn">
$lte
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/gt/#mongodb-query-op.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/gte/#mongodb-query-op.-gte">
<code class="leafygreen-ui-1l06pbn">
$gte
</code>
</a>
operators.
</p>
<p class="leafygreen-ui-1kp3ins">
For details on how the following options affect security and performance, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/#std-label-qe-field-configuration">
Configure Encrypted Fields for Optimal Search and Storage
<!-- -->
.
</a>
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Add the
<code class="leafygreen-ui-1l06pbn">
queries
</code>
object and set
<code class="leafygreen-ui-1l06pbn">
queryType
</code>
to
<code class="leafygreen-ui-1l06pbn">
"range"
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n path: \"myDocumentRangeField\",\n bsonType: \"int\",\n queries: { queryType: \"range\" }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"myDocumentRangeField"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"int"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queries</span>: <!-- -->{ <span class="lg-highlight-attr">queryType</span>: <span class="lg-highlight-string">"range"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:30%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/#std-label-qe-field-min-max">
min and max
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Same as field
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Required if
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
is
<code class="leafygreen-ui-1l06pbn">
decimal
</code>
or
<code class="leafygreen-ui-1l06pbn">
double
</code>
. Optional but highly recommended if it is
<code class="leafygreen-ui-1l06pbn">
int
</code>
,
<code class="leafygreen-ui-1l06pbn">
long
</code>
, or
<code class="leafygreen-ui-1l06pbn">
date
</code>
. Defaults to the minimum and maximum
values of the
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
When possible, specifying bounds on a query improves
performance. If querying values outside of these inclusive
bounds, MongoDB returns an error.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n path: \"myDocumentRangeField\",\n bsonType: \"int\",\n queries: { queryType: \"range\",\n min: 0,\n max: 1200\n }\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"myDocumentRangeField"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"int"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queries</span>: <!-- -->{ <span class="lg-highlight-attr">queryType</span>: <span class="lg-highlight-string">"range"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">min</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">max</span>: <span class="lg-highlight-number">1200</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ol>
</section>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This example shows how to create an encryption schema for hospital data.
</p>
<p class="leafygreen-ui-1kp3ins">
Consider the following document that contains personally identifiable information
(PII), credit card information, and sensitive medical information:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"firstName\": \"Jon\",\n \"lastName\": \"Snow\",\n \"patientId\": 12345187,\n \"address\": \"123 Cherry Ave\",\n \"medications\": [\n \"Adderall\",\n \"Lipitor\"\n ],\n \"patientInfo\": {\n \"ssn\": \"921-12-1234\",\n \"billing\": {\n \"type\": \"visa\",\n \"number\": \"1234-1234-1234-1234\"\n }\n }\n}","programmingLanguage":"JSON"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"firstName"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"Jon"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"lastName"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"Snow"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"patientId"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">12345187</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"address"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"123 Cherry Ave"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"medications"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Adderall"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Lipitor"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">]</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"patientInfo"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"ssn"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"921-12-1234"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"billing"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"type"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"visa"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"number"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"1234-1234-1234-1234"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To ensure the PII and sensitive medical information stays secure, this
encryption schema adds the relevant fields:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const encryptedFieldsObject = {\n fields: [\n {\n path: \"patientId\",\n bsonType: \"int\"\n },\n {\n path: \"patientInfo.ssn\",\n bsonType: \"string\"\n },\n {\n path: \"medications\",\n bsonType: \"array\"\n },\n {\n path: \"patientInfo.billing\",\n bsonType: \"object\"\n }\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> encryptedFieldsObject <!-- -->= <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">fields</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"patientId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"int"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"patientInfo.ssn"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"string"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"medications"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"array"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"patientInfo.billing"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"object"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Adding the
<code class="leafygreen-ui-1l06pbn">
queries
</code>
property makes the
<code class="leafygreen-ui-1l06pbn">
patientId
</code>
and
<code class="leafygreen-ui-1l06pbn">
patientInfo.ssn
</code>
fields queryable. This example enables equality queries:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const encryptedFieldsObject = {\n fields: [\n {\n path: \"patientId\",\n bsonType: \"int\",\n queries: { queryType: \"equality\" }\n },\n {\n path: \"patientInfo.ssn\",\n bsonType: \"string\",\n queries: { queryType: \"equality\" }\n },\n {\n path: \"medications\",\n bsonType: \"array\"\n },\n {\n path: \"patientInfo.billing\",\n bsonType: \"object\"\n },\n ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> encryptedFieldsObject <!-- -->= <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">fields</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"patientId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"int"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queries</span>: <!-- -->{ <span class="lg-highlight-attr">queryType</span>: <span class="lg-highlight-string">"equality"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"patientInfo.ssn"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"string"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queries</span>: <!-- -->{ <span class="lg-highlight-attr">queryType</span>: <span class="lg-highlight-string">"equality"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"medications"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"array"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">path</span>: <span class="lg-highlight-string">"patientInfo.billing"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bsonType</span>: <span class="lg-highlight-string">"object"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/dropIndexes/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
dropIndexes
<a class="headerlink leafygreen-ui-1kepx47" href="#dropindexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="dropindexes">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
<span>
Command Fields
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.dropIndexes" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.dropIndexes">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
command drops one or more indexes
(except the index on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field and the last remaining shard
key index, if one exists) from the specified collection.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
In
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#std-program-mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
, this command can also be run through the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndex/#mongodb-method-db.collection.dropIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.dropIndex()
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndexes/#mongodb-method-db.collection.dropIndexes">
<code class="leafygreen-ui-1l06pbn">
db.collection.dropIndexes()
</code>
</a>
helper methods.
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
Helper methods are convenient for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#std-program-mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
users, but they may not
return the same level of information as database commands. In cases where
the convenience is not needed or the additional return fields are required,
use the database command.
</p>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is supported in all MongoDB Atlas clusters.
For information on Atlas support for all commands, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n dropIndexes: <string>,\n index: <string|document|arrayofstrings>,\n writeConcern: <document>, comment: <any>\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">dropIndexes</span>: <<!-- -->string<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">index</span>: <!-- --><string|document|arrayofstrings>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> writeConcern: <document>, comment: <any></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command takes the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:10%"/>
<col style="width:20%"/>
<col style="width:70%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
dropIndexes
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
String
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The name of the collection whose indexes to drop.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
index
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string or document or array of strings
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The index or indexes to drop.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To drop all indexes except the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
index and the last
remaining shard key index from the collection if one
exists, specify
<code class="leafygreen-ui-1l06pbn">
"*"
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To drop a single index, specify either the index name,
the index specification document (unless the index is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-text/#std-label-index-type-text">
text
</a>
index), or an array of the
index name. To drop a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-text/#std-label-index-type-text">
text
</a>
index,
specify the index names instead of the index specification
document. If this index is the last remaining shard key
index,
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
raises an error.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To drop multiple indexes, specify an array of the index names.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
writeConcern
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. A document expressing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/">
write concern
</a>
of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/drop/#mongodb-dbcommand-dbcmd.drop">
<code class="leafygreen-ui-1l06pbn">
drop
</code>
</a>
command.
Omit to use the default write concern.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
comment
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
any
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. A user-provided comment to attach to this command. Once set, this
comment appears alongside records of this command in the following
locations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-ref">
mongod log messages
</a>
, in the
<code class="leafygreen-ui-1l06pbn">
attr.command.cursor.comment
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
Database profiler
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#mongodb-data-system.profile.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
<code class="leafygreen-ui-1l06pbn">
currentOp
</code>
</a>
output, in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-data-currentOp.command">
<code class="leafygreen-ui-1l06pbn">
command.comment
</code>
</a>
field.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
A comment can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON type
</a>
(string, integer, object, array, etc).
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
raises an error if you attempt
to use it to remove the last remaining shard key compatible index.
Passing
<code class="leafygreen-ui-1l06pbn">
"*"
</code>
to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
drops all indexes except
the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
index and the last remaining shard key compatible index,
if one exists.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.2, you can use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
to drop existing
indexes on the same collection even if there is a build in progress on
another index. In earlier versions, attempting to drop a different
index during an in-progress index build results in a
<code class="leafygreen-ui-1l06pbn">
BackgroundOperationInProgressForNamespace
</code>
error.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Kill related queries only
<a class="headerlink leafygreen-ui-1kepx47" href="#kill-related-queries-only" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="kill-related-queries-only">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
operation only kills queries that are using the index being
dropped. This may include queries considering the index as part of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
query planning
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Resource Locking
<a class="headerlink leafygreen-ui-1kepx47" href="#resource-locking" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resource-locking">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
obtains an exclusive lock on the specified collection
for the duration of the operation. All subsequent operations on the
collection must wait until
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
releases the
lock.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Index Names
<a class="headerlink leafygreen-ui-1kepx47" href="#index-names" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="index-names">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the method is passed an array of index names that includes a
non-existent index, the method errors without dropping any of the
specified indexes.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
_id
</code>
Index
<a class="headerlink leafygreen-ui-1kepx47" href="#_id-index" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="_id-index">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You cannot drop the default index on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
text Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#text-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="text-indexes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To drop a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-text/#std-label-index-type-text">
text
</a>
index, specify the index name
instead of the index specification document.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-dropIndexes-cmd-index-builds">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stop In-Progress Index Builds
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-in-progress-index-builds" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stop-in-progress-index-builds">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If an index specified to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
is still building,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
attempts
to stop the in-progress build. Stopping an index build has the same effect as
dropping the built index.
</p>
<p class="leafygreen-ui-1kp3ins">
For replica sets, run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.dropIndexes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</span>
</a>
on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
.
The primary stops the index build and creates an associated
"abortIndexBuild"
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
entry. Secondaries which replicate
the "abortIndexBuild" oplog entry stop the in-progress index build and
discard the build job. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-creation/#std-label-index-build-process">
Index Build Process
</a>
for detailed
documentation on the index build process.
</p>
<p class="leafygreen-ui-1kp3ins">
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
<code class="leafygreen-ui-1l06pbn">
currentOp
</code>
</a>
to identify the index builds associated with
a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndexes/#mongodb-method-db.collection.createIndexes">
<code class="leafygreen-ui-1l06pbn">
db.collection.createIndexes()
</code>
</a>
operation. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#std-label-currentOp-cmd-index-creation">
Active Indexing Operations
</a>
for an example.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Hidden Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#hidden-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="hidden-indexes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB offers the ability to hide or unhide indexes from the query planner.
By hiding an index from the planner, you can evaluate the potential impact of
dropping an index without actually dropping the index.
</p>
<p class="leafygreen-ui-1kp3ins">
If after the evaluation, the user decides to drop the index, you
can drop the hidden index; i.e. you do not need to unhide it first to
drop it.
</p>
<p class="leafygreen-ui-1kp3ins">
If, however, the impact is negative, the user can unhide the index
instead of having to recreate a dropped index. And because indexes are
fully maintained while hidden, the indexes are immediately available
for use once unhidden.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on hidden indexes, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-hidden/">
Hidden Indexes
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To drop all non-
<code class="leafygreen-ui-1l06pbn">
_id
</code>
indexes , specify
<code class="leafygreen-ui-1l06pbn">
"*"
</code>
for the
<code class="leafygreen-ui-1l06pbn">
index
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { dropIndexes: \"collection\", index: \"*\" } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{ <span class="lg-highlight-attr">dropIndexes</span>: <span class="lg-highlight-string">"collection"</span>, <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-string">"*"</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To drop a single index, issue the command by specifying the name of
the index you want to drop. For example, to drop the index named
<code class="leafygreen-ui-1l06pbn">
age_1
</code>
, use the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { dropIndexes: \"collection\", index: \"age_1\" })","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{ <span class="lg-highlight-attr">dropIndexes</span>: <span class="lg-highlight-string">"collection"</span>, <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-string">"age_1"</span> })</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
provides the helper methods
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndex/#mongodb-method-db.collection.dropIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.dropIndex()
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndexes/#mongodb-method-db.collection.dropIndexes">
<code class="leafygreen-ui-1l06pbn">
db.collection.dropIndexes()
</code>
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.dropIndex(\"age_1\");","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">dropIndex</span>(<span class="lg-highlight-string">"age_1"</span>)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To drop multiple indexes, issue the command by specifying an array of
the index names:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { dropIndexes: \"collection\", index: [ \"age_1\", \"age_1_status_1\" ] } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{ <span class="lg-highlight-attr">dropIndexes</span>: <span class="lg-highlight-string">"collection"</span>, <span class="lg-highlight-attr">index</span>: <!-- -->[ <span class="lg-highlight-string">"age_1"</span>, <span class="lg-highlight-string">"age_1_status_1"</span> ] } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ul>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndexes/#mongodb-method-db.collection.dropIndexes">
<code class="leafygreen-ui-1l06pbn">
db.collection.dropIndexes()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndex/#mongodb-method-db.collection.dropIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.dropIndex()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/isdbgrid/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
isdbgrid
<a class="headerlink leafygreen-ui-1kepx47" href="#isdbgrid" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="isdbgrid">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output" target="_self">
<span>
Output
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
isdbgrid
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.isdbgrid" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.isdbgrid">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
This command verifies that a process is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If you issue the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.isdbgrid" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
isdbgrid
</code>
</span>
</a>
command when connected to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
, the response document includes the
<code class="leafygreen-ui-1l06pbn">
isdbgrid
</code>
field set to
<code class="leafygreen-ui-1l06pbn">
1
</code>
.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is supported in all MongoDB Atlas clusters.
For information on Atlas support for all commands, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n isdbgrid: 1\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isdbgrid</span>: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Output
<a class="headerlink leafygreen-ui-1kepx47" href="#output" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="output">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The returned document is similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"isdbgrid\" : 1, \"hostname\" : \"app.example.net\", \"ok\" : 1 }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"isdbgrid"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"hostname"</span> <!-- -->: <span class="lg-highlight-string">"app.example.net"</span>, <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you issue the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.isdbgrid" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
isdbgrid
</code>
</span>
</a>
command when connected to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
, MongoDB returns an error document. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.isdbgrid" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
isdbgrid
</code>
</span>
</a>
command is not available to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
.
The error document, however, also includes a line that reads
<code class="leafygreen-ui-1l06pbn">
"isdbgrid" : 1
</code>
, just as in the document returned for a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
. The error document is similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"errmsg\" : \"no such cmd: isdbgrid\",\n \"bad cmd\" : {\n \"isdbgrid\" : 1\n },\n \"ok\" : 0\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"errmsg"</span> <!-- -->: <span class="lg-highlight-string">"no such cmd: isdbgrid"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"bad cmd"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"isdbgrid"</span> <!-- -->: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">0</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You can instead use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/hello/#mongodb-dbcommand-dbcmd.hello">
<code class="leafygreen-ui-1l06pbn">
hello
</code>
</a>
command to determine
connection to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
. When connected to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/hello/#mongodb-dbcommand-dbcmd.hello">
<code class="leafygreen-ui-1l06pbn">
hello
</code>
</a>
command returns a document that
contains the string
<code class="leafygreen-ui-1l06pbn">
isdbgrid
</code>
in the
<code class="leafygreen-ui-1l06pbn">
msg
</code>
field.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.serverCmdLineOpts/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.serverCmdLineOpts()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.servercmdlineopts--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="db.servercmdlineopts--">
</div>
</a>
</h1>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.serverCmdLineOpts()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.serverCmdLineOpts" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-db.serverCmdLineOpts">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Wraps the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getCmdLineOpts/#mongodb-dbcommand-dbcmd.getCmdLineOpts">
<code class="leafygreen-ui-1l06pbn">
getCmdLineOpts
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-database-command">
database command
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Returns a document that reports on the arguments and configuration
options used to start the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
or
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
instance.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#std-label-configuration-options">
Self-Managed Configuration File Options
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
for additional information on
available MongoDB runtime options.
</p>
</dd>
</dl>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/reIndex/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
reIndex
<a class="headerlink leafygreen-ui-1kepx47" href="#reindex" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="reindex">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
<span>
Command Fields
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
reIndex
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.reIndex" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.reIndex">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
Deprecated
<!-- -->
since version
<!-- -->
6.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Attempting to run the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.reIndex" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
reIndex
</code>
</span>
</a>
command writes a warning
message to the log.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
In
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#std-program-mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
, this command can also be run through the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.reIndex/#mongodb-method-db.collection.reIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.reIndex()
</code>
</a>
helper method
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
Helper methods are convenient for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#std-program-mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
users, but they may not
return the same level of information as database commands. In cases where
the convenience is not needed or the additional return fields are required,
use the database command.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.reIndex" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
reIndex
</code>
</span>
</a>
command drops all indexes on a
collection and recreates them. This operation may be expensive for
collections that have a large amount of data and/or a large number
of indexes.
</p>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.reIndex" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
reIndex
</code>
</span>
</a>
may only be run on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-standalone">
standalone
</a>
instances.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For most users, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.reIndex" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
reIndex
</code>
</span>
</a>
command is unnecessary.
</p>
</li>
</ul>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is not supported in MongoDB Atlas clusters.
For information on Atlas support for all commands,
see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n reIndex: <collection>\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">reIndex</span>: <<!-- -->collection<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command takes the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
reIndex
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The name of the collection to reindex.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For MongoDB 5.0 or later,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.reIndex" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
reIndex
</code>
</span>
</a>
may only be run on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-standalone">
standalone
</a>
instances.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Resource Locking
<a class="headerlink leafygreen-ui-1kepx47" href="#resource-locking" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resource-locking">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.reIndex" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
reIndex
</code>
</span>
</a>
obtains an exclusive (W) lock on the collection and blocks
other operations on the collection until it completes.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on locking in MongoDB, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/faq/concurrency/">
FAQ: Concurrency
<!-- -->
.
</a>
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-creation/">
Index Builds on Populated Collections
</a>
for more information on the
behavior of indexing operations in MongoDB.
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/upgrade-to-enterprise-replica-set/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Upgrade to MongoDB Enterprise (Replica Set)
<a class="headerlink leafygreen-ui-1kepx47" href="#upgrade-to-mongodb-enterprise--replica-set-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="upgrade-to-mongodb-enterprise--replica-set-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#consideration" target="_self">
<span>
Consideration
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#download-enterprise-binaries" target="_self">
<span>
Download Enterprise Binaries
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
MongoDB Enterprise provides various features not available in the
MongoDB Community edition, such as:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/inmemory/">
In-Memory Storage Engine for Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/auditing/">
Auditing Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/kerberos/">
Kerberos Authentication on Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap/">
Self-Managed LDAP Proxy Authentication
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-ldap-external/">
LDAP Authorization on Self-Managed Deployments
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-encryption-at-rest/">
Encryption at Rest
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The following steps outline the procedure to upgrade a replica set from
the MongoDB Community Edition to the MongoDB Enterprise Edition. For
example, the steps can be used to upgrade MongoDB 7.0 Community to
MongoDB 7.0 Enterprise.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Consideration
<a class="headerlink leafygreen-ui-1kepx47" href="#consideration" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="consideration">
</div>
</a>
</h2>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Do not use these instructions to upgrade to another release version.
To upgrade release versions, refer to the appropriate release
upgrade instructions, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/8.0/#std-label-8.0-upgrade">
Upgrade to MongoDB 8.0
<!-- -->
.
</a>
</p>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Download Enterprise Binaries
<a class="headerlink leafygreen-ui-1kepx47" href="#download-enterprise-binaries" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="download-enterprise-binaries">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Depending on your operating system, you can install the MongoDB
Enterprise binaries by using a package manager or by downloading the
binaries manually.
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-6">
<div aria-label="Tabs to describe usage of linux-manual/linux-package/macos/windows" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_3xyfecgbp" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_9ods5s1sq" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Linux (Package Manager)
</div>
</button>
<button aria-controls="_71nwv2cia" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_o7lj58s7t" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Linux (Manual Download)
</div>
</button>
<button aria-controls="_23mj64lm7" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_apxhgc28u" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Windows
</div>
</button>
<button aria-controls="_wu8jvkitc" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_0pqmeiem4" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
macOS
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_9qwuxjmpj" class="" id="_uz69zyey0" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If you have installed MongoDB Community using a package
manager, follow the package manager instructions for your
operating system:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-red-hat/#std-label-install-mdb-enterprise-redhat-centos">
Red Hat Enterprise or CentOS
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-ubuntu/#std-label-install-mdb-enterprise-ubuntu">
Ubuntu
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-debian/#std-label-install-mdb-enterprise-debian">
Debian
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-suse/#std-label-install-mdb-enterprise-suse">
SUSE
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-amazon/#std-label-install-mdb-enterprise-amazon">
Amazon Linux
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
During the installation, the package manager will remove the
community packages; this will not affect the running
deployment until you restart.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_skvdwzdrl" class="leafygreen-ui-38lglc" id="_weevmmypx" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If you have not installed MongoDB using a package manager,
you can manually download the MongoDB binaries from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/try/download/enterprise?tck=docs_server" target="_self">
<span>
MongoDB Download Center
</span>
</a>
. Follow
the manual instructions, including specific prerequisites
for MongoDB Enterprise, for your operating system:
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Install to a location different from that of your current
Community edition.
</p>
<p class="leafygreen-ui-1kp3ins">
In the upgrade procedure, you will use the existing data
directory and, if applicable, the existing configuration
file.
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-red-hat-tarball/#std-label-install-mdb-enterprise-redhat-centos-tgz">
Red Hat Enterprise or CentOS
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-ubuntu-tarball/#std-label-install-mdb-enterprise-ubuntu-tgz">
Ubuntu
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-debian-tarball/#std-label-install-mdb-enterprise-debian-tgz">
Debian
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-suse-tarball/#std-label-install-mdb-enterprise-suse-tgz">
SUSE
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-amazon-tarball/#std-label-install-mdb-enterprise-amazon-tgz">
Amazon Linux
</a>
</p>
</li>
</ul>
</div>
</div>
</div>
<div>
<div aria-labelledby="_2greo44ns" class="leafygreen-ui-38lglc" id="_wngjd70i1" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
You cannot use the
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
to install the Enterprise
edition if you have the Community edition of the same
release series installed on the same machine. That is, if
you have version 4.4.0 Community edition installed, you
cannot use the
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
to install the 4.4.0 or 4.4.1
Enterprise edition.
</p>
</div>
</div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Manually download the archive file from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/try/download/enterprise?tck=docs_server" target="_self">
<span>
MongoDB
Download Center
<!-- -->
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To install, extract/unzip the file to a location
different from that of your current Community
edition.
</p>
<p class="leafygreen-ui-1kp3ins">
In the upgrade procedure, you will use the existing
data directory and, if applicable, the existing
configuration file.
</p>
</li>
</ol>
</div>
</div>
</div>
<div>
<div aria-labelledby="_fyttdjy06" class="leafygreen-ui-38lglc" id="_sraj2gyy8" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
Install the binaries:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Manually download the archive file from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/try/download/enterprise?tck=docs_server" target="_self">
<span>
MongoDB
Download Center
<!-- -->
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Extract the files to a location different
from that of your current Community edition. For
details on extracting the files, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-enterprise-on-os-x/">
macOS
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
In the upgrade procedure, you will use the existing
data directory and, if applicable, the existing
configuration file.
</p>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To minimize downtime, you can upgrade from MongoDB Community to
Enterprise Edition using a "rolling" upgrade by upgrading the members
individually while the other members are available.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Upgrade secondary members of the replica set.Upgrade the secondary members\nof the replica set one at a time:Shut down the mongod instance.Restart with the Enterprise mongod, specifying the\nsame configuration options (e.g. same data directory,\nconfiguration file, etc.).Wait for the member to recover to SECONDARY state before\nupgrading the next secondary member."},{"@type":"HowToStep","text":"Step down the replica set primary.Once all the secondary members have been upgraded to Enterprise,\nconnect mongosh to the primary and use\nrs.stepDown() to step down the primary and force an\nelection of a new primary."},{"@type":"HowToStep","text":"Upgrade the primary.When rs.status()\nshows that the primary has stepped down and another member\nhas assumed PRIMARY state, upgrade the stepped-down primary:Shut down the stepped-down primary.Restart with the Enterprise mongod, specifying the\nsame configuration options (e.g. same data directory,\nconfiguration file, etc.)."}],"name":"Procedure","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Upgrade secondary members of the replica set.
<a class="headerlink leafygreen-ui-1kepx47" href="#upgrade-secondary-members-of-the-replica-set" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="upgrade-secondary-members-of-the-replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Upgrade the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-members/#std-label-replica-set-secondary-members">
secondary
</a>
members
of the replica set one at a time:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Shut down the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart with the Enterprise
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
, specifying the
same configuration options (e.g. same data directory,
configuration file, etc.).
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
Wait for the member to recover to
<code class="leafygreen-ui-1l06pbn">
SECONDARY
</code>
state before
upgrading the next secondary member.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Step down the replica set primary.
<a class="headerlink leafygreen-ui-1kepx47" href="#step-down-the-replica-set-primary" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="step-down-the-replica-set-primary">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Once all the secondary members have been upgraded to Enterprise,
connect
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
to the primary and use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.stepDown/#mongodb-method-rs.stepDown">
<code class="leafygreen-ui-1l06pbn">
rs.stepDown()
</code>
</a>
to step down the primary and force an
election of a new primary.
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Upgrade the primary.
<a class="headerlink leafygreen-ui-1kepx47" href="#upgrade-the-primary" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="upgrade-the-primary">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.status/#mongodb-method-rs.status">
<code class="leafygreen-ui-1l06pbn">
rs.status()
</code>
</a>
shows that the primary has stepped down and another member
has assumed
<code class="leafygreen-ui-1l06pbn">
PRIMARY
</code>
state, upgrade the stepped-down primary:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Shut down the stepped-down primary.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart with the Enterprise
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
, specifying the
same configuration options (e.g. same data directory,
configuration file, etc.).
</p>
</li>
</ol>
</section>
</div>
</div>
</div>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Before using any Enterprise features, ensure that all members have
been upgraded to Enterprise edition.
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Encrypted Fields and Enabled Queries
<a class="headerlink leafygreen-ui-1kepx47" href="#encrypted-fields-and-enabled-queries" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="encrypted-fields-and-enabled-queries">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#supported-query-types-and-behavior" target="_self">
<span>
Supported Query Types and Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#client-and-server-schema-validation" target="_self">
<span>
Client and Server Schema Validation
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#considerations-when-enabling-querying" target="_self">
<span>
Considerations when Enabling Querying
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#configure-encrypted-fields-for-optimal-search-and-storage" target="_self">
<span>
Configure Encrypted Fields for Optimal Search and Storage
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
When you use Queryable Encryption, you define encrypted fields at the collection level
using an encryption schema. Encrypting a field and enabling queries
increases storage requirements and impacts query performance. You can
configure an encrypted field for either equality or range queries,
but not both. Configure fields for the expected query type.
</p>
<p class="leafygreen-ui-1kp3ins">
For instructions on creating an encryption schema and configuring
querying, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/qe-create-encryption-schema/#std-label-qe-create-encryption-schema">
Create an Encryption Schema
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-query-types">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Supported Query Types and Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#supported-query-types-and-behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="supported-query-types-and-behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For a list of supported query operators and behavior with encrypted fields, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/reference/supported-operations/#std-label-qe-supported-query-operators">
Supported Query Operators
<!-- -->
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Client and Server Schema Validation
<a class="headerlink leafygreen-ui-1kepx47" href="#client-and-server-schema-validation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="client-and-server-schema-validation">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB supports using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/schema-validation/#std-label-schema-validation-overview">
schema validation
</a>
to enforce encryption of specific fields in a collection. Clients using
automatic Queryable Encryption behave differently depending on the database connection configuration:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the connection
<code class="leafygreen-ui-1l06pbn">
encryptedFieldsMap
</code>
object contains a key for the
specified collection, the client uses that object to perform
automatic Queryable Encryption, rather than using the remote schema. At minimum,
the local rules must encrypt all fields that the remote schema does.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the connection
<code class="leafygreen-ui-1l06pbn">
encryptedFieldsMap
</code>
object doesn't contain a
key for the specified collection, the client downloads the
server-side remote schema for the collection and uses it instead.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Remote Schema Behavior
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
When using a remote schema:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The client trusts that the server has a valid schema
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The client uses the remote schema to perform automatic Queryable Encryption
only. The client does not enforce any other validation rules
specified in the schema.
</p>
</li>
</ul>
</div>
</div>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Considerations when Enabling Querying
<a class="headerlink leafygreen-ui-1kepx47" href="#considerations-when-enabling-querying" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="considerations-when-enabling-querying">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can make an encrypted field queryable. To change which fields are encrypted
or queryable, rebuild the collection's encryption schema and re-create the
collection.
</p>
<p class="leafygreen-ui-1kp3ins">
If you don't need to query an encrypted field, you may not need to enable
querying on that field. You can still retrieve the document by querying other
fields that are queryable or unencrypted.
</p>
<p class="leafygreen-ui-1kp3ins">
For every encrypted collection, MongoDB creates
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/manage-collections/#std-label-qe-metadata-collections">
two metadata
collections
</a>
, increasing storage space. MongoDB
creates an index for each encrypted field, which increases the duration of
write operations on that field. When a write operation updates an indexed
field, MongoDB updates the related index.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-field-configuration">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Configure Encrypted Fields for Optimal Search and Storage
<a class="headerlink leafygreen-ui-1kepx47" href="#configure-encrypted-fields-for-optimal-search-and-storage" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="configure-encrypted-fields-for-optimal-search-and-storage">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB provides the following parameters to facilitate debugging
and performance tuning:
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-field-min-max">
</span>
<dl class="parameter">
<dt>
<code class="leafygreen-ui-1l06pbn">
min, max
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-parameter-param.min--max" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-parameter-param.min--max">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<em>
Query Type
</em>
:
<!-- -->
Range queries only.
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
Type
</em>
: Must match the field's
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Required if
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
is
<code class="leafygreen-ui-1l06pbn">
decimal
</code>
or
<code class="leafygreen-ui-1l06pbn">
double
</code>
. Optional
but highly recommended if it is
<code class="leafygreen-ui-1l06pbn">
int
</code>
,
<code class="leafygreen-ui-1l06pbn">
long
</code>
, or
<code class="leafygreen-ui-1l06pbn">
date
</code>
. Defaults to
the
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
min and max values.
</p>
<p class="leafygreen-ui-1kp3ins">
Specify minimum and maximum (inclusive) queryable values for a
field when possible, as smaller bounds improve query efficiency. If querying
values outside of these bounds, MongoDB returns an error.
</p>
</dd>
</dl>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-qe-field-sparsity" target="_self">
<span>
sparsity
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-qe-field-precision" target="_self">
<span>
precision
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-qe-field-trimfactor" target="_self">
<span>
trimFactor
</span>
</a>
,
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-qe-contention" target="_self">
<span>
contention
</span>
</a>
parameters are intended for advanced users only. The
default values for these options are suitable for the majority
of use cases, and should only be modified if your use case
requires it.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-field-sparsity">
</span>
<dl class="parameter">
<dt>
<code class="leafygreen-ui-1l06pbn">
sparsity
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-parameter-param.sparsity" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-parameter-param.sparsity">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<em>
Query Type
</em>
:
<!-- -->
Range queries only.
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
Type
</em>
: Integer from
<code class="leafygreen-ui-1l06pbn">
1
</code>
-
<code class="leafygreen-ui-1l06pbn">
4
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Optional. Defaults to
<code class="leafygreen-ui-1l06pbn">
2
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Affects how thoroughly MongoDB indexes range values. Low sparsity (dense
indexing) improves query performance, but stores more documents in the
encrypted metadata collections for each insert or update operation, causing
greater storage overhead. High sparsity does the opposite.
</p>
</dd>
</dl>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-field-precision">
</span>
<dl class="parameter">
<dt>
<code class="leafygreen-ui-1l06pbn">
precision
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-parameter-param.precision" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-parameter-param.precision">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<em>
Query Type
</em>
:
<!-- -->
Range queries only.
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
Type
</em>
: Integer.
</p>
<p class="leafygreen-ui-1kp3ins">
Optional. Allowed only if
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
is
<code class="leafygreen-ui-1l06pbn">
double
</code>
or
<code class="leafygreen-ui-1l06pbn">
decimal
</code>
.
If unset, MongoDB uses the same maximum precision as the
<code class="leafygreen-ui-1l06pbn">
bsonType
</code>
,
either
<code class="leafygreen-ui-1l06pbn">
double
</code>
or
<code class="leafygreen-ui-1l06pbn">
decimal
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Determines how many digits are taken into account when querying a
<code class="leafygreen-ui-1l06pbn">
double
</code>
or
<code class="leafygreen-ui-1l06pbn">
decimal
</code>
field. Every additional digit increases storage
overhead, and has a high impact on searchable range and index generation.
This parameter bounds the precision of range queries. Different
<code class="leafygreen-ui-1l06pbn">
precision
</code>
values affect the query output, though encrypted values are
still stored with full precision.
</p>
</dd>
</dl>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-field-trimfactor">
</span>
<dl class="parameter">
<dt>
<code class="leafygreen-ui-1l06pbn">
trimFactor
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-parameter-param.trimFactor" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-parameter-param.trimFactor">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<em>
Query Type
</em>
:
<!-- -->
Range queries only.
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
Type
</em>
: Integer.
</p>
<p class="leafygreen-ui-1kp3ins">
Optional. Defaults to
<code class="leafygreen-ui-1l06pbn">
6
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
trimFactor
</code>
controls the throughput of concurrent inserts and updates.
A higher
<code class="leafygreen-ui-1l06pbn">
trimFactor
</code>
increases the throughput of concurrent insert and
updates at the cost of slowing down some range read operations. A lower
<code class="leafygreen-ui-1l06pbn">
trimFactor
</code>
does the opposite.
</p>
</dd>
</dl>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-contention">
</span>
<dl class="parameter">
<dt>
<code class="leafygreen-ui-1l06pbn">
contention
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-parameter-param.contention" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-parameter-param.contention">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<em>
Query Type
</em>
:
<!-- -->
Equality and range queries.
</p>
<p class="leafygreen-ui-1kp3ins">
<em>
Type
</em>
: Integer.
</p>
<p class="leafygreen-ui-1kp3ins">
Optional. Defaults to
<code class="leafygreen-ui-1l06pbn">
8
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Concurrent write operations, such as inserting the same field/value pair into
multiple documents in close succession, can cause contention: conflicts
that delay operations.
</p>
<p class="leafygreen-ui-1kp3ins">
With Queryable Encryption, MongoDB tracks the occurrences of each field/value pair in an
encrypted collection using an internal counter. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-contention-factor">
contention factor
</a>
partitions this counter, similar to an array. This minimizes issues with
incrementing the counter when using
<code class="leafygreen-ui-1l06pbn">
insert
</code>
,
<code class="leafygreen-ui-1l06pbn">
update
</code>
, or
<code class="leafygreen-ui-1l06pbn">
findAndModify
</code>
to add or modify an encrypted field
with the same field/value pair in close succession.
<code class="leafygreen-ui-1l06pbn">
contention = 0
</code>
creates an array with one element at index 0.
<code class="leafygreen-ui-1l06pbn">
contention = 4
</code>
creates an
array with 5 elements at indexes 0-4. MongoDB increments a random array element
during insert.
</p>
<p class="leafygreen-ui-1kp3ins">
When unset,
<code class="leafygreen-ui-1l06pbn">
contention
</code>
defaults to
<code class="leafygreen-ui-1l06pbn">
8
</code>
, which provides high performance
for most workloads. Higher contention improves the performance of insert and
update operations on low cardinality fields, but decreases find performance.
</p>
<p class="leafygreen-ui-1kp3ins">
You can optionally include
<code class="leafygreen-ui-1l06pbn">
contention
</code>
on queryable fields to change the
value from its default of
<code class="leafygreen-ui-1l06pbn">
8
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For more thorough information on contention factor and its cryptographic
implications, see "Section 9: Guidelines" in MongoDB's
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/resources/products/capabilities/queryable-encryption-technical-paper" target="_self">
<span>
Queryable Encryption Technical Paper
</span>
</a>
.
</p>
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/lockInfo/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
lockInfo
<a class="headerlink leafygreen-ui-1kepx47" href="#lockinfo" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="lockinfo">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output-example" target="_self">
<span>
Output Example
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#output-fields" target="_self">
<span>
Output Fields
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
lockInfo
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.lockInfo" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.lockInfo">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns information on locks that are currently being held or
pending.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.lockInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
lockInfo
</code>
</span>
</a>
is an internal command available on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances only.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is not supported in M0, M2, and M5 clusters.
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand(\n {\n lockInfo: 1\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">lockInfo</span>: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Output Example
<a class="headerlink leafygreen-ui-1kepx47" href="#output-example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="output-example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following is an example of the output from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.lockInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
lockInfo
</code>
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"lockInfo\" : [\n {\n \"resourceId\" : \"{2305843009213693953: ParallelBatchWriterMode, 1}\",\n \"granted\" : [\n {\n \"mode\" : \"IS\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"{ findandmodify: \\\"hugeindex\\\", query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(\\\"dc611138-04c6-49b1-a7ac-161040dd9d65\\\") }, $db: \\\"test\\\" }\",\n \"clientInfo\" : {\n \"desc\" : \"conn3\",\n \"connectionId\" : 3,\n \"client\" : \"127.0.0.1:55165\",\n \"opid\" : 28770\n }\n }\n ],\n \"pending\" : [ ]\n },\n {\n \"resourceId\" : \"{6917529027641081857: Global, 1}\",\n \"granted\" : [\n {\n \"mode\" : \"IX\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"{ findandmodify: \\\"hugeindex\\\", query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(\\\"dc611138-04c6-49b1-a7ac-161040dd9d65\\\") }, $db: \\\"test\\\" }\",\n \"clientInfo\" : {\n \"desc\" : \"conn3\",\n \"connectionId\" : 3,\n \"client\" : \"127.0.0.1:55165\",\n \"opid\" : 28770\n }\n },\n {\n \"mode\" : \"IX\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"index build: 29d48366-63ad-41e2-a689-69255a89c094\",\n \"clientInfo\" : {\n \"desc\" : \"IndexBuildsCoordinatorMongod-0\",\n \"opid\" : 27437\n }\n }\n ],\n \"pending\" : [ ]\n },\n {\n \"resourceId\" : \"{4611686018427387905: ReplicationStateTransition, 1}\",\n \"granted\" : [\n {\n \"mode\" : \"IX\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"{ findandmodify: \\\"hugeindex\\\", query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(\\\"dc611138-04c6-49b1-a7ac-161040dd9d65\\\") }, $db: \\\"test\\\" }\",\n \"clientInfo\" : {\n \"desc\" : \"conn3\",\n \"connectionId\" : 3,\n \"client\" : \"127.0.0.1:55165\",\n \"opid\" : 28770\n }\n },\n {\n \"mode\" : \"IX\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"index build: 29d48366-63ad-41e2-a689-69255a89c094\",\n \"clientInfo\" : {\n \"desc\" : \"IndexBuildsCoordinatorMongod-0\",\n \"opid\" : 27437\n }\n }\n ],\n \"pending\" : [ ]\n },\n {\n \"resourceId\" : \"{10123292395995783581: Database, 899920359141007773, test}\",\n \"granted\" : [\n {\n \"mode\" : \"IX\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"{ findandmodify: \\\"hugeindex\\\", query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(\\\"dc611138-04c6-49b1-a7ac-161040dd9d65\\\") }, $db: \\\"test\\\" }\",\n \"clientInfo\" : {\n \"desc\" : \"conn3\",\n \"connectionId\" : 3,\n \"client\" : \"127.0.0.1:55165\",\n \"opid\" : 28770\n }\n },\n {\n \"mode\" : \"IX\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"index build: 29d48366-63ad-41e2-a689-69255a89c094\",\n \"clientInfo\" : {\n \"desc\" : \"IndexBuildsCoordinatorMongod-0\",\n \"opid\" : 27437\n }\n }\n ],\n \"pending\" : [ ]\n },\n {\n \"resourceId\" : \"{12576221391967629173: Collection, 1047006345899159413, test.hugeindex}\",\n \"granted\" : [\n {\n \"mode\" : \"IX\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"{ findandmodify: \\\"hugeindex\\\", query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(\\\"dc611138-04c6-49b1-a7ac-161040dd9d65\\\") }, $db: \\\"test\\\" }\",\n \"clientInfo\" : {\n \"desc\" : \"conn3\",\n \"connectionId\" : 3,\n \"client\" : \"127.0.0.1:55165\",\n \"opid\" : 28770\n }\n },\n {\n \"mode\" : \"IX\",\n \"convertMode\" : \"NONE\",\n \"enqueueAtFront\" : false,\n \"compatibleFirst\" : false,\n \"debugInfo\" : \"index build: 29d48366-63ad-41e2-a689-69255a89c094\",\n \"clientInfo\" : {\n \"desc\" : \"IndexBuildsCoordinatorMongod-0\",\n \"opid\" : 27437\n }\n }\n ],\n \"pending\" : [ ]\n }\n ],\n \"ok\" : 1\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"lockInfo"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resourceId"</span> <!-- -->: <span class="lg-highlight-string">"{2305843009213693953: ParallelBatchWriterMode, 1}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"granted"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IS"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"{ findandmodify: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">hugeindex</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">, query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">dc611138-04c6-49b1-a7ac-161040dd9d65</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">) }, $db: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">test</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string"> }"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"conn3"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connectionId"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"client"</span> <!-- -->: <span class="lg-highlight-string">"127.0.0.1:55165"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">28770</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"pending"</span> <!-- -->: <!-- -->[ <!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resourceId"</span> <!-- -->: <span class="lg-highlight-string">"{6917529027641081857: Global, 1}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"granted"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IX"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"{ findandmodify: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">hugeindex</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">, query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">dc611138-04c6-49b1-a7ac-161040dd9d65</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">) }, $db: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">test</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string"> }"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"conn3"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connectionId"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"client"</span> <!-- -->: <span class="lg-highlight-string">"127.0.0.1:55165"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">28770</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IX"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"index build: 29d48366-63ad-41e2-a689-69255a89c094"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"IndexBuildsCoordinatorMongod-0"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">27437</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"pending"</span> <!-- -->: <!-- -->[ <!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resourceId"</span> <!-- -->: <span class="lg-highlight-string">"{4611686018427387905: ReplicationStateTransition, 1}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"granted"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IX"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"{ findandmodify: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">hugeindex</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">, query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">dc611138-04c6-49b1-a7ac-161040dd9d65</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">) }, $db: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">test</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string"> }"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"conn3"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connectionId"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"client"</span> <!-- -->: <span class="lg-highlight-string">"127.0.0.1:55165"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">28770</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IX"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"index build: 29d48366-63ad-41e2-a689-69255a89c094"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"IndexBuildsCoordinatorMongod-0"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">27437</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"pending"</span> <!-- -->: <!-- -->[ <!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resourceId"</span> <!-- -->: <span class="lg-highlight-string">"{10123292395995783581: Database, 899920359141007773, test}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"granted"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IX"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"{ findandmodify: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">hugeindex</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">, query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">dc611138-04c6-49b1-a7ac-161040dd9d65</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">) }, $db: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">test</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string"> }"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"conn3"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connectionId"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"client"</span> <!-- -->: <span class="lg-highlight-string">"127.0.0.1:55165"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">28770</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IX"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"index build: 29d48366-63ad-41e2-a689-69255a89c094"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"IndexBuildsCoordinatorMongod-0"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">27437</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"pending"</span> <!-- -->: <!-- -->[ <!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resourceId"</span> <!-- -->: <span class="lg-highlight-string">"{12576221391967629173: Collection, 1047006345899159413, test.hugeindex}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"granted"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IX"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"{ findandmodify: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">hugeindex</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">, query: { _id: 585.0 }, update: { $pop: { a: 1.0 } }, upsert: false, new: false, lsid: { id: UUID(</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">dc611138-04c6-49b1-a7ac-161040dd9d65</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">) }, $db: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">test</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string"> }"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"conn3"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"connectionId"</span> <!-- -->: <span class="lg-highlight-number">3</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"client"</span> <!-- -->: <span class="lg-highlight-string">"127.0.0.1:55165"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">28770</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"mode"</span> <!-- -->: <span class="lg-highlight-string">"IX"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"convertMode"</span> <!-- -->: <span class="lg-highlight-string">"NONE"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enqueueAtFront"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"compatibleFirst"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"debugInfo"</span> <!-- -->: <span class="lg-highlight-string">"index build: 29d48366-63ad-41e2-a689-69255a89c094"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"clientInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"desc"</span> <!-- -->: <span class="lg-highlight-string">"IndexBuildsCoordinatorMongod-0"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"opid"</span> <!-- -->: <span class="lg-highlight-number">27437</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"pending"</span> <!-- -->: <!-- -->[ <!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ok"</span> <!-- -->: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Output Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#output-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="output-fields">
</div>
</a>
</h2>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
lockInfo
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-lockInfo" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-lockInfo">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
An array of documents that report on the lock information. Each
document includes:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-lockInfo.resourceId" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
resourceId
</code>
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-lockInfo.granted" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
granted
</code>
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-lockInfo.pending" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
pending
</code>
</span>
</a>
</p>
</li>
</ul>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
lockInfo.resourceId
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-lockInfo.resourceId" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-lockInfo.resourceId">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The resource on which the locks are being held or pending.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
lockInfo.granted
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-lockInfo.granted" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-lockInfo.granted">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
An array of documents. Each document provides
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lockinformation" target="_self">
<span>
information on
locks
</span>
</a>
that are currently granted on the resource.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
lockInfo.pending
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-lockInfo.pending" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-data-lockInfo.pending">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
An array of documents. Each document provides
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lockinformation" target="_self">
<span>
information on
locks
</span>
</a>
that are currently pending on the resource.
</p>
</dd>
</dl>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lockinformation">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Lock Information
<a class="headerlink leafygreen-ui-1kepx47" href="#lock-information" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="lock-information">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For each
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-lockInfo.granted" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
granted
</code>
</span>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-lockInfo.pending" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
pending
</code>
</span>
</a>
lock
document, information include:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
mode
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Specifies the lock mode:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Lock Mode
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
S
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shared
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
X
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Exclusive
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
IS
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Intent Shared
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
IX
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Intent Exclusive (IX)
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
convertMode
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the new lock mode for conversion.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
enqueueAtFront
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A boolean that indicates whether to put the lock at the front
of the queue or the back in case of conflict.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
compatibleFirst
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A boolean that indicates to grant lock requests based on
compatibility with already granted locks or to use the
first-in-first-out (FIFO) order.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
debugInfo
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Information about the operation that issued the lock request.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
clientInfo
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A document detailing the client information.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceAll/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$replaceAll (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-replaceall--aggregation-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-replaceall--aggregation-">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-replaceAll" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-expression-exp.-replaceAll">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Replaces all instances of a search string in an input string with a
replacement string.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
is both case-sensitive and
diacritic-sensitive, and ignores any collation present on a
collection.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
operator has the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-agg-quick-ref-operator-expressions">
operator expression syntax
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $replaceAll: { input: <expression>, find: <expression>, replacement: <expression> } }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$replaceAll</span>: <!-- -->{ <span class="lg-highlight-attr">input</span>: <<!-- -->expression<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">find</span>: <<!-- -->expression<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">replacement</span>: <<!-- -->expression<!-- -->> <!-- -->} }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Operator Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#operator-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="operator-fields">
</div>
</a>
</h3>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replaceAll-input">
</span>
<p class="leafygreen-ui-1kp3ins">
The string on which you wish to apply the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
. Can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a
string or a
<code class="leafygreen-ui-1l06pbn">
null
</code>
. If
<code class="leafygreen-ui-1l06pbn">
input
</code>
refers to a field that is
missing,
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replaceAll-find">
</span>
<p class="leafygreen-ui-1kp3ins">
The string to search for within the given
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
. Can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a
string or a
<code class="leafygreen-ui-1l06pbn">
null
</code>
. If
<code class="leafygreen-ui-1l06pbn">
find
</code>
refers to a field that is
missing,
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-replacement" target="_self">
<span>
replacement
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replaceAll-replacement">
</span>
<p class="leafygreen-ui-1kp3ins">
The string to use to replace all matched instances of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
.
Can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that
resolves to a string or a
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
, and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-replacement" target="_self">
<span>
replacement
</span>
</a>
expressions must evaluate to
a string or a
<code class="leafygreen-ui-1l06pbn">
null
</code>
, or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
fails with an
error.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replaceall-and-null-values">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
and Null Values
<a class="headerlink leafygreen-ui-1kepx47" href="#-replaceall-and-null-values" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-replaceall-and-null-values">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
refer to a field that is missing, they return
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If
<em>
any one
</em>
of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
, or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-replacement" target="_self">
<span>
replacement
</span>
</a>
evaluates to a
<code class="leafygreen-ui-1l06pbn">
null
</code>
, the
entire
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
expression evaluates to
<code class="leafygreen-ui-1l06pbn">
null
</code>
:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:90%"/>
<col style="width:10%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Result
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $replaceAll: { input: null, find: "abc", replacement: "ABC" } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
null
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $replaceAll: { input: "abc", find: null, replacement: "ABC" } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
null
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $replaceAll: { input: "abc", find: "abc", replacement: null } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
null
</code>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replaceall-and-collation">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
and Collation
<a class="headerlink leafygreen-ui-1kepx47" href="#-replaceall-and-collation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-replaceall-and-collation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
String matching for all
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
expressions is always
case-sensitive and diacritic-sensitive. Any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
configured on a collection,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
db.collection.aggregate()
</code>
</a>
, or
index is ignored when performing string comparisons with
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, create a sample collection with collation strength
<code class="leafygreen-ui-1l06pbn">
1
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection( \"myColl\", { collation: { locale: \"fr\", strength: 1 } } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createCollection</span>( <span class="lg-highlight-string">"myColl"</span>, <!-- -->{ <span class="lg-highlight-attr">collation</span>: <!-- -->{ <span class="lg-highlight-attr">locale</span>: <span class="lg-highlight-string">"fr"</span>, <span class="lg-highlight-attr">strength</span>: <span class="lg-highlight-number">1</span> } } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
A collation strength of
<code class="leafygreen-ui-1l06pbn">
1
</code>
compares base character only and ignores
other differences such as case and diacritics.
</p>
<p class="leafygreen-ui-1kp3ins">
Next, insert three example documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.myColl.insertMany([\n { _id: 1, name: \"cafe\" },\n { _id: 2, name: \"Cafe\" },\n { _id: 3, name: \"café\" }\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">myColl</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"cafe"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Cafe"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"café"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
operation tries to find and
replace all instances of "Cafe" in the
<code class="leafygreen-ui-1l06pbn">
name
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.myColl.aggregate([\n {\n $addFields:\n {\n resultObject: { $replaceAll: { input: \"$name\", find: \"Cafe\", replacement: \"CAFE\" } }\n }\n }\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">myColl</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$addFields</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">resultObject</span>: <!-- -->{ <span class="lg-highlight-attr">$replaceAll</span>: <!-- -->{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$name"</span>, <span class="lg-highlight-attr">find</span>: <span class="lg-highlight-string">"Cafe"</span>, <span class="lg-highlight-attr">replacement</span>: <span class="lg-highlight-string">"CAFE"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Because
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
ignores the collation configured for
this collection, the operation only matches the instance of "Cafe" in
document
<code class="leafygreen-ui-1l06pbn">
2
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"name\" : \"cafe\", \"resultObject\" : \"cafe\" }\n{ \"_id\" : 2, \"name\" : \"Cafe\", \"resultObject\" : \"CAFE\" }\n{ \"_id\" : 3, \"name\" : \"café\", \"resultObject\" : \"café\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"cafe"</span>, <span class="lg-highlight-string">"resultObject"</span> <!-- -->: <span class="lg-highlight-string">"cafe"</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Cafe"</span>, <span class="lg-highlight-string">"resultObject"</span> <!-- -->: <span class="lg-highlight-string">"CAFE"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"café"</span>, <span class="lg-highlight-string">"resultObject"</span> <!-- -->: <span class="lg-highlight-string">"café"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Operators which respect collation, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
, would
match all three documents when performing a string comparison against
"Cafe" due to this collection's collation strength of
<code class="leafygreen-ui-1l06pbn">
1
</code>
.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replaceall-and-unicode-normalization">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
and Unicode Normalization
<a class="headerlink leafygreen-ui-1kepx47" href="#-replaceall-and-unicode-normalization" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-replaceall-and-unicode-normalization">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
aggregation expression does not perform
any unicode normalization. This means that string matching for all
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
expressions will consider the number of code points used
to represent a character in unicode when attempting a match.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the character
<code class="leafygreen-ui-1l06pbn">
é
</code>
can be represented in unicode using
either one code point or two:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:25%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Unicode
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Displays as
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Code points
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
\xe9
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
é
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1 (
<code class="leafygreen-ui-1l06pbn">
\xe9
</code>
)
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
e\u0301
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
é
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2 (
<code class="leafygreen-ui-1l06pbn">
e
</code>
+
<code class="leafygreen-ui-1l06pbn">
\u0301
</code>
)
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
Using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
with a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
string where the character
<code class="leafygreen-ui-1l06pbn">
é
</code>
is represented in unicode with one code
point will not match any instance of
<code class="leafygreen-ui-1l06pbn">
é
</code>
that uses two code points in
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
string.
</p>
<p class="leafygreen-ui-1kp3ins">
The following table shows whether a match occurs for a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
string of "café" when compared to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
strings where
<code class="leafygreen-ui-1l06pbn">
é
</code>
is represented
by either one code point or two. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
string in this example uses one code point to represent the
<code class="leafygreen-ui-1l06pbn">
é
</code>
character:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:85%"/>
<col style="width:15%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Match
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $replaceAll: { input: "caf\xe9", find: "café", replacement: "CAFE" } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
yes
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $replaceAll: { input: "cafe\u0301", find: "café", replacement: "CAFE" } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
no
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
Because
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-replaceAll" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</span>
</a>
does not perform any unicode
normalization, only the first string comparison matches, where both the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-find" target="_self">
<span>
find
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-replaceAll-input" target="_self">
<span>
input
</span>
</a>
strings use one code point to represent
<code class="leafygreen-ui-1l06pbn">
é
</code>
.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Create an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.insertMany([\n { \"_id\" : 1, \"item\" : \"blue paint\" },\n { \"_id\" : 2, \"item\" : \"blue and green paint\" },\n { \"_id\" : 3, \"item\" : \"blue paint with blue paintbrush\" },\n { \"_id\" : 4, \"item\" : \"blue paint with green paintbrush\" },\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"blue paint"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"blue and green paint"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"blue paint with blue paintbrush"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"blue paint with green paintbrush"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following example replaces each instance of "blue paint" in the
<code class="leafygreen-ui-1l06pbn">
item
</code>
field with "red paint":
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.aggregate([\n {\n $project:\n {\n item: { $replaceAll: { input: \"$item\", find: \"blue paint\", replacement: \"red paint\" } }\n }\n }\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$project</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">item</span>: <!-- -->{ <span class="lg-highlight-attr">$replaceAll</span>: <!-- -->{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$item"</span>, <span class="lg-highlight-attr">find</span>: <span class="lg-highlight-string">"blue paint"</span>, <span class="lg-highlight-attr">replacement</span>: <span class="lg-highlight-string">"red paint"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns the following results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"item\" : \"red paint\" }\n{ \"_id\" : 2, \"item\" : \"blue and green paint\" }\n{ \"_id\" : 3, \"item\" : \"red paint with red paintbrush\" }\n{ \"_id\" : 4, \"item\" : \"red paint with green paintbrush\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"red paint"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"blue and green paint"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"red paint with red paintbrush"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"red paint with green paintbrush"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Bulk.insert/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Bulk.insert()
<a class="headerlink leafygreen-ui-1kepx47" href="#bulk.insert--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="bulk.insert--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
MongoDB also provides the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.bulkWrite/#mongodb-method-db.collection.bulkWrite">
<code class="leafygreen-ui-1l06pbn">
db.collection.bulkWrite()
</code>
</a>
method for
performing bulk write operations.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
Bulk.insert(<document>)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Bulk.insert" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-Bulk.insert">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Adds an insert operation to a bulk operations list.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Bulk.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
Bulk.insert()
</code>
</span>
</a>
accepts the following parameter:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
doc
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Document to insert. The size of the document must be less than or
equal to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-BSON-Document-Size">
maximum BSON document size
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Insert Inaccuracies
<a class="headerlink leafygreen-ui-1kepx47" href="#insert-inaccuracies" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="insert-inaccuracies">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Even if you encounter a server error during an insert, some documents
may have been inserted.
</p>
<p class="leafygreen-ui-1kp3ins">
After a successful insert, the system returns
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/BulkWriteResult/#mongodb-data-BulkWriteResult.insertedCount">
<code class="leafygreen-ui-1l06pbn">
BulkWriteResult.insertedCount
</code>
</a>
, the number
of documents inserted into the collection. If the insert
operation is interrupted by a replica set state change,
the system may continue inserting documents. As a result,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/BulkWriteResult/#mongodb-data-BulkWriteResult.insertedCount">
<code class="leafygreen-ui-1l06pbn">
BulkWriteResult.insertedCount
</code>
</a>
may report fewer documents than actually inserted.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Performance Consideration for Random Data
<a class="headerlink leafygreen-ui-1kepx47" href="#performance-consideration-for-random-data" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="performance-consideration-for-random-data">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If an operation inserts a large amount of random data (for example,
hashed indexes) on an indexed field, insert performance may decrease.
Bulk inserts of random data create random index entries, which increase
the size of the index. If the index reaches the size that requires each
random insert to access a different index entry, the inserts result in a
high rate of WiredTiger cache eviction and replacement. When this
happens, the index is no longer fully in cache and is updated on disk,
which decreases performance.
</p>
<p class="leafygreen-ui-1kp3ins">
To improve the performance of bulk inserts of random data on indexed
fields, you can either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Drop the index, then recreate it after you insert the random data.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Insert the data into an empty unindexed collection.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Creating the index after the bulk insert sorts the data in memory and
performs an ordered insert on all indexes.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following initializes a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk/#mongodb-method-Bulk">
<code class="leafygreen-ui-1l06pbn">
Bulk()
</code>
</a>
operations builder for the
<code class="leafygreen-ui-1l06pbn">
items
</code>
collection and adds a series of insert operations to add
multiple documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var bulk = db.items.initializeUnorderedBulkOp();\nbulk.insert( { item: \"abc123\", defaultQty: 100, status: \"A\", points: 100 } );\nbulk.insert( { item: \"ijk123\", defaultQty: 200, status: \"A\", points: 200 } );\nbulk.insert( { item: \"mop123\", defaultQty: 0, status: \"P\", points: 0 } );\nbulk.execute();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> bulk <!-- -->= <!-- -->db.<span class="lg-highlight-property">items</span>.<span class="lg-highlight-title lg-highlight-function">initializeUnorderedBulkOp</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">insert</span>( <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"abc123"</span>, <span class="lg-highlight-attr">defaultQty</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">100</span> } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">insert</span>( <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"ijk123"</span>, <span class="lg-highlight-attr">defaultQty</span>: <span class="lg-highlight-number">200</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">200</span> } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">insert</span>( <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"mop123"</span>, <span class="lg-highlight-attr">defaultQty</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"P"</span>, <span class="lg-highlight-attr">points</span>: <span class="lg-highlight-number">0</span> } )<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bulk.<span class="lg-highlight-title lg-highlight-function">execute</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.initializeUnorderedBulkOp/#mongodb-method-db.collection.initializeUnorderedBulkOp">
<code class="leafygreen-ui-1l06pbn">
db.collection.initializeUnorderedBulkOp()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.initializeOrderedBulkOp/#mongodb-method-db.collection.initializeOrderedBulkOp">
<code class="leafygreen-ui-1l06pbn">
db.collection.initializeOrderedBulkOp()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.execute/#mongodb-method-Bulk.execute">
<code class="leafygreen-ui-1l06pbn">
Bulk.execute()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.getUser/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.getUser()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.getuser--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="db.getuser--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#required-access" target="_self">
<span>
Required Access
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.getUser(username, args)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.getUser" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-db.getUser">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns user information for a specified user. Run this method on the
user's database. If the user doesn't exist in the database,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.getUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.getUser()
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.getUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.getUser()
</code>
</span>
</a>
method has the following parameters:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getUser( \"<username>\", {\n showCredentials: <boolean>,\n showCustomData: <boolean>,\n showPrivileges: <boolean>,\n showAuthenticationRestrictions: <boolean>,\n filter: <document>\n} )"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.getUser( "<username>", {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> showCredentials: <Boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> showCustomData: <Boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> showPrivileges: <Boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> showAuthenticationRestrictions: <Boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> filter: <document></td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Parameter
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
username
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The name of the user for which to retrieve information.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
args
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. A document specifying additional arguments.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
args
</code>
document supports the following fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
showCredentials
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. Set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
to display the user's password
hash.
</p>
<p class="leafygreen-ui-1kp3ins">
By default, this field is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
showCustomData
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. Set to
<code class="leafygreen-ui-1l06pbn">
false
</code>
to omit the user's
<code class="leafygreen-ui-1l06pbn">
customData
</code>
from the output.
</p>
<p class="leafygreen-ui-1kp3ins">
By default, this field is
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
showPrivileges
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. Set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
to show the user's full set
of privileges, including expanded information for the
inherited roles.
</p>
<p class="leafygreen-ui-1kp3ins">
By default, this field is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If viewing all users, you cannot specify this field.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
showAuthenticationRestrictions
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
boolean
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. Set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
to show the user's
authentication restrictions.
</p>
<p class="leafygreen-ui-1kp3ins">
By default, this field is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If viewing all users, you cannot specify this field.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
filter
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. A document that specifies
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
stage
conditions to return information for users that match the
filter conditions.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.getUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.getUser()
</code>
</span>
</a>
wraps the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/usersInfo/#mongodb-dbcommand-dbcmd.usersInfo">
<code class="leafygreen-ui-1l06pbn">
usersInfo: <username>
</code>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
For details on output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/usersInfo/#mongodb-dbcommand-dbcmd.usersInfo">
<code class="leafygreen-ui-1l06pbn">
usersInfo
</code>
.
</a>
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Required Access
<a class="headerlink leafygreen-ui-1kepx47" href="#required-access" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="required-access">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To view another user's information, you must have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-viewUser">
<code class="leafygreen-ui-1l06pbn">
viewUser
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
action
</a>
on the
other user's database.
</p>
<p class="leafygreen-ui-1kp3ins">
Users can view their own information.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following operations return information about an example
<code class="leafygreen-ui-1l06pbn">
appClient
</code>
user in an
<code class="leafygreen-ui-1l06pbn">
accounts
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use accounts\ndb.getUser(\"appClient\")","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use accounts</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getUser</span>(<span class="lg-highlight-string">"appClient"</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 'accounts.appClient',\n userId: UUID(\"1c2fc1bf-c4dc-4a22-8b04-3971349ce0dc\"),\n user: 'appClient',\n db: 'accounts',\n roles: [],\n mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'accounts.appClient'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">userId</span>: <span class="lg-highlight-title lg-highlight-function">UUID</span>(<span class="lg-highlight-string">"1c2fc1bf-c4dc-4a22-8b04-3971349ce0dc"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">'appClient'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">'accounts'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">mechanisms</span>: <!-- -->[ <span class="lg-highlight-string">'SCRAM-SHA-1'</span>, <span class="lg-highlight-string">'SCRAM-SHA-256'</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Omit Custom Data from Output
<a class="headerlink leafygreen-ui-1kepx47" href="#omit-custom-data-from-output" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="omit-custom-data-from-output">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
:
<!-- -->
To omit a user's custom data from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getUser/#mongodb-method-db.getUser">
<code class="leafygreen-ui-1l06pbn">
db.getUser()
</code>
</a>
output,
set the
<code class="leafygreen-ui-1l06pbn">
showCustomData
</code>
option to
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createUser/#mongodb-dbcommand-dbcmd.createUser">
<code class="leafygreen-ui-1l06pbn">
createUser
</code>
</a>
command to create a user named
<code class="leafygreen-ui-1l06pbn">
accountAdmin01
</code>
on the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"products\").runCommand( {\n createUser: \"accountAdmin01\",\n pwd: passwordPrompt(),\n customData: { employeeId: 12345 },\n roles: [ { role: 'readWrite', db: 'products' } ]\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"products"</span>).<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">createUser</span>: <span class="lg-highlight-string">"accountAdmin01"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pwd</span>: <span class="lg-highlight-title lg-highlight-function">passwordPrompt</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">customData</span>: <!-- -->{ <span class="lg-highlight-attr">employeeId</span>: <span class="lg-highlight-number">12345</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">'readWrite'</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">'products'</span> } ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The user contains a
<code class="leafygreen-ui-1l06pbn">
customData
</code>
field of
<code class="leafygreen-ui-1l06pbn">
{ employeeId: 12345 }
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
To retrieve the user but omit the custom data from the output, run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.getUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.getUser()
</code>
</span>
</a>
with
<code class="leafygreen-ui-1l06pbn">
showCustomData
</code>
set to
<code class="leafygreen-ui-1l06pbn">
false
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"products\").getUser(\n \"accountAdmin01\",\n { showCustomData: false }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"products"</span>).<span class="lg-highlight-title lg-highlight-function">getUser</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"accountAdmin01"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">showCustomData</span>: <span class="lg-highlight-literal">false</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Example output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 'products.accountAdmin01',\n userId: UUID(\"0955afc1-303c-4683-a029-8e17dd5501f4\"),\n user: 'accountAdmin01',\n db: 'products',\n roles: [ { role: 'readWrite', db: 'products' } ],\n mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'products.accountAdmin01'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">userId</span>: <span class="lg-highlight-title lg-highlight-function">UUID</span>(<span class="lg-highlight-string">"0955afc1-303c-4683-a029-8e17dd5501f4"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">'accountAdmin01'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">'products'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">'readWrite'</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">'products'</span> } ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">mechanisms</span>: <!-- -->[ <span class="lg-highlight-string">'SCRAM-SHA-1'</span>, <span class="lg-highlight-string">'SCRAM-SHA-256'</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/indexes/index-properties/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Index Properties
<a class="headerlink leafygreen-ui-1kepx47" href="#index-properties" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="index-properties">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#case-insensitive-indexes" target="_self">
<span>
Case-Insensitive Indexes
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#hidden-indexes" target="_self">
<span>
Hidden Indexes
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#partial-indexes" target="_self">
<span>
Partial Indexes
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#sparse-indexes" target="_self">
<span>
Sparse Indexes
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#ttl-indexes" target="_self">
<span>
TTL Indexes
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#unique-indexes" target="_self">
<span>
Unique Indexes
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Index properties affect how the query planner uses an index and how
indexed documents are stored. You can specify index properties as
optional parameters when you create an index.
</p>
<p class="leafygreen-ui-1kp3ins">
The following sections explain the index properties that you can specify
when building an index.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Not all index types are compatible with all index properties.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Case-Insensitive Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#case-insensitive-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="case-insensitive-indexes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-case-insensitive/#std-label-index-feature-case-insensitive">
Case-insensitive indexes
</a>
support
queries on strings without considering letter case.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Hidden Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#hidden-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="hidden-indexes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-hidden/#std-label-index-type-hidden">
Hidden indexes
</a>
are not visible to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
query planner
</a>
and cannot be used
to support a query.
</p>
<p class="leafygreen-ui-1kp3ins">
You can use hidden indexes to evaluate the potential impact of dropping
an index without actually dropping it. If the impact is negative, you
can unhide the index instead of having to recreate a dropped index.
Hidden indexes are fully maintained and can be used immediately once
unhidden.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-partial-index">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Partial Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#partial-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="partial-indexes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-partial/#std-label-index-type-partial">
Partial indexes
</a>
only index the documents in
a collection that meet a specified filter expression. Partial indexes
have lower storage requirements and reduced performance costs for index
creation and maintenance.
</p>
<p class="leafygreen-ui-1kp3ins">
Partial indexes offer a superset of the functionality of sparse indexes
and should be preferred over sparse indexes.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Sparse Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#sparse-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sparse-indexes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-sparse/#std-label-index-type-sparse">
Sparse indexes
</a>
only contain entries for
documents that have the indexed field. These indexes skip documents that
do not have the indexed field.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-ttl-index">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
TTL Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#ttl-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="ttl-indexes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-ttl/#std-label-index-feature-ttl">
TTL indexes
</a>
automatically remove documents
from a collection after a certain amount of time. Use these indexes for
data that only needs to persist for a finite amount of time, like
machine generated event data, logs, and session information.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-unique-index">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Unique Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#unique-indexes" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="unique-indexes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
Unique indexes
</a>
cause MongoDB to reject
duplicate values for the indexed field. These indexes are useful when
your documents contain a unique identifier, such as a
<code class="leafygreen-ui-1l06pbn">
userId
</code>
.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/timeseries/timeseries-bucketing/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
About Time Series Data
<a class="headerlink leafygreen-ui-1kepx47" href="#about-time-series-data" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="about-time-series-data">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#properties-of-time-series-data" target="_self">
<span>
Properties of Time Series Data
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#comparing-time-series-collections-to-regular-collections" target="_self">
<span>
Comparing Time Series Collections to Regular Collections
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#how-bucketing-works" target="_self">
<span>
How Bucketing Works
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#how-the-metafield-affects-bucketing" target="_self">
<span>
How the metaField Affects Bucketing
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#the-bucket-catalog" target="_self">
<span>
The Bucket Catalog
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#creation" target="_self">
<span>
Creation
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#closure" target="_self">
<span>
Closure
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#deletion" target="_self">
<span>
Deletion
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Internally, MongoDB optimizes time series data by grouping documents in a time
series collection based on common
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
values. Selecting a useful one
provides significant optimizations to storage density and query performance.
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-timeseries-collections-metafield">
metaFields
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-timeseries-data-properties">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Properties of Time Series Data
<a class="headerlink leafygreen-ui-1kepx47" href="#properties-of-time-series-data" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="properties-of-time-series-data">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Time series data has several properties that differentiate it from other data
formats:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Documents arrive in order, requiring frequent insert operations to append
them.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update operations are rare, since each document represents a single point in
time.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Delete operations are rare if your application benefits from having
an extensive historical record.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Data is indexed by time and an identifier, such as a stock ticker, that
identifies the unique time series it belongs to.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Data is high volume, since each individual time series requires a large,
constantly growing number of documents.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To account for these factors, MongoDB uses a specialized columnar format that
groups documents from each time series together. This has the following
benefits:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reduced storage and index sizes
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Improved query efficiency
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reduced I/O for read operations
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Increased usage of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
in-memory
cache, further improving query speed
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reduced complexity for working with time series data
</p>
</li>
</ul>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Comparing Time Series Collections to Regular Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#comparing-time-series-collections-to-regular-collections" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="comparing-time-series-collections-to-regular-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In a regular collection, data is stored sequentially as blocks on disk,
optimizing write speed. However, it requires one index for each data point,
which quickly grows very large. It also requires a second index that contains
the time series identifier and timestamps themselves, so that users can query
for a single series. To read this data, MongoDB has to process all of the
database and disk blocks that contain it, even if a block only contains a
single relevant document.
</p>
<p class="leafygreen-ui-1kp3ins">
This model is optimized for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-CRUD">
CRUD
</a>
operations and frequent updates. A
bank account balance only needs to reflect the current state, so each account
holder's document is updated as that information changes.
</p>
<p class="leafygreen-ui-1kp3ins">
Compare this to a time series collection. Time series collections write data in
order, meaning recent transactions can be kept in memory for much faster
retrieval. Since they are written in sequence, documents are stored
together, so there's no need to read every disk block after a document
is no longer in memory. Data is indexed by the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
, leading to much
smaller indexes.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-timeseries-bucketing-specifics">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
How Bucketing Works
<a class="headerlink leafygreen-ui-1kepx47" href="#how-bucketing-works" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="how-bucketing-works">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When you create a time series collection, MongoDB automatically creates
a
<code class="leafygreen-ui-1l06pbn">
system.buckets
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-collections/#std-label-metadata-system-collections">
system collection
</a>
.
MongoDB groups documents that have both:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An identical
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
value, which should uniquely identify a time
series. If a
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
is an object or array, MongoDB groups only if all
object fields or array elements match.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
values that are close together. The time series collection's
<code class="leafygreen-ui-1l06pbn">
granularity
</code>
,
<code class="leafygreen-ui-1l06pbn">
bucketMaxSpanSeconds
</code>
, and
<code class="leafygreen-ui-1l06pbn">
bucketRoundingSeconds
</code>
parameters control the time span covered by each bucket. For more
information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries/timeseries-granularity/#std-label-timeseries-granularity">
Set Granularity for Time Series Data
<!-- -->
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, with a granularity of
<code class="leafygreen-ui-1l06pbn">
seconds
</code>
, MongoDB buckets documents
within the same hour. If a bucket contains a document with a
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
value of
<code class="leafygreen-ui-1l06pbn">
sensorA
</code>
and a
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
value of
<code class="leafygreen-ui-1l06pbn">
2024-08-01T18:23:21Z
</code>
, an
incoming document with a
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
of
<code class="leafygreen-ui-1l06pbn">
sensorB
</code>
goes into a separate
bucket regardless of time. An incoming document from
<code class="leafygreen-ui-1l06pbn">
sensorA
</code>
goes into the
same bucket only if its
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
is between
<code class="leafygreen-ui-1l06pbn">
2024-08-01T18:00:00Z
</code>
and
<code class="leafygreen-ui-1l06pbn">
2024-08-01T18:59:59Z
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If a document with a time of
<code class="leafygreen-ui-1l06pbn">
2023-03-27T16:24:35Z
</code>
does not fit an
existing bucket, MongoDB creates a new bucket with a minimum time of
<code class="leafygreen-ui-1l06pbn">
2023-03-27T16:00:00Z
</code>
and a maximum time of
<code class="leafygreen-ui-1l06pbn">
2023-03-27T19:59:59Z
</code>
.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
You can modify a time series collection's granularity, but only to change
from finer to coarser measurements, such as extending bucket coverage
from minutes hours. This updates the collection's view definition, but
doesn't change how data is stored across existing buckets.
</p>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
How the metaField Affects Bucketing
<a class="headerlink leafygreen-ui-1kepx47" href="#how-the-metafield-affects-bucketing" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="how-the-metafield-affects-bucketing">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Because
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
values must match exactly to group documents,
the number of buckets in a time series collection depends on the number of
unique
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
values. Collections with fine-grained or changing
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
values generate many sparsely packed, short-lived buckets.
This leads to decreased storage and query efficiency.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, in the following document,
<code class="leafygreen-ui-1l06pbn">
metadata
</code>
is a good choice
of
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
since it makes it easy to query data from a given weather
sensor. Using these fields, MongoDB buckets readings from a single sensor together.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n timestamp: ISODate(\"2021-05-18T00:00:00.000Z\"),\n metadata: { sensorId: 5578, type: 'temperature' },\n temp: 12,\n _id: ObjectId(\"62f11bbf1e52f124b84479ad\")\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timestamp</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-05-18T00:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">metadata</span>: <!-- -->{ <span class="lg-highlight-attr">sensorId</span>: <span class="lg-highlight-number">5578</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'temperature'</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">temp</span>: <span class="lg-highlight-number">12</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"62f11bbf1e52f124b84479ad"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-timeseries-bucket-catalog">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-timeseries-bucket-lifecycle">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
The Bucket Catalog
<a class="headerlink leafygreen-ui-1kepx47" href="#the-bucket-catalog" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="the-bucket-catalog">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The bucket catalog is a specialized in-memory cache in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
. It tracks buckets to minimize latency and coordinate
concurrent writes.
</p>
<p class="leafygreen-ui-1kp3ins">
For each open bucket, the catalog maintains information such as the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
, active writers, covered time span, number of documents, size,
and recent operations. Because MongoDB creates separate buckets for documents
with a different
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
, multiple buckets are typically open at the same
time.
</p>
<p class="leafygreen-ui-1kp3ins">
To avoid inconsistencies caused by race conditions, buckets may be closed and
removed from the bucket catalog when a conflicting operation is executed.
Restarting
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
closes all buckets and resets the bucket
catalog.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Creation
<a class="headerlink leafygreen-ui-1kepx47" href="#creation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="creation">
</div>
</a>
</h3>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB creates a new bucket if there isn't a suitable one for an incoming
document. This occurs when any of the following are true:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The document
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
doesn't match any active buckets.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The document timestamp is outside of the range of all active buckets.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The document exceeds the remaining size or document limit of all
active buckets.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The starting timestamp of a new bucket is rounded down based on the
collection's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries/timeseries-granularity/#std-label-timeseries-granularity">
granularity
</a>
. This handles cases
where documents with out-of-order timestamps arrive in close succession.
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-timeseries-bucket-closure">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Closure
<a class="headerlink leafygreen-ui-1kepx47" href="#closure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="closure">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB closes a bucket under any of the following circumstances:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Time has moved forward or backward past the covered time span, as
indicated by an incoming document timestamp that falls outside of the
bucket's bounds. These bounds are determined by the collection's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries/timeseries-granularity/#std-label-timeseries-granularity">
granularity
</a>
setting.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The bucket has hit the document limit (default 1000).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The bucket has exceeded its storage size limit. This happens when:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The size exceeds the allowed maximum (default 125KiB).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The number of documents is below a minimum number (default 10) and the
size is below 12MiB.
</p>
<p class="leafygreen-ui-1kp3ins">
This is a set, internal limit that optimizes performance when data
consists of fewer, larger documents.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The set of active buckets doesn't fit within the allowed storage engine
cache size. You can review this information using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-dbcommand-dbcmd.collStats">
<code class="leafygreen-ui-1l06pbn">
collStats
</code>
</a>
database command.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The bucket catalog exceeds its allowed total memory allocation (by default,
2.5% of available system memory)
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A conflicting operation, such as a chunk migration or update, changes a
bucket's on-disk state.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
restarts. This closes all buckets.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Deletion
<a class="headerlink leafygreen-ui-1kepx47" href="#deletion" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="deletion">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB deletes a bucket when:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Its maximum allowed timestamp is less than the current time minus
the collection's
<code class="leafygreen-ui-1l06pbn">
expireAfterSeconds
</code>
parameter. This is equivalent to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/expire-data/#std-label-ttl-collections">
TTL collection's
</a>
time to live.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/delete/#mongodb-dbcommand-dbcmd.delete">
<code class="leafygreen-ui-1l06pbn">
delete
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.deleteMany/#mongodb-method-db.collection.deleteMany">
<code class="leafygreen-ui-1l06pbn">
db.collection.deleteMany()
</code>
</a>
command deletes
the last document in the bucket.
</p>
</li>
</ul>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/configure-x509-client-authentication/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Use x.509 Certificates to Authenticate Clients on Self-Managed Deployments
<a class="headerlink leafygreen-ui-1kepx47" href="#use-x.509-certificates-to-authenticate-clients-on-self-managed-deployments" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="use-x.509-certificates-to-authenticate-clients-on-self-managed-deployments">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#prerequisites" target="_self">
<span>
Prerequisites
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#procedure" target="_self">
<span>
Procedure
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#next-steps" target="_self">
<span>
Next Steps
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
The following procedure sets up x.509 certificate authentication for
client authentication on a standalone
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance.
This is also known as Mutual TLS or mTLS.
</p>
<p class="leafygreen-ui-1kp3ins">
To use x.509 authentication for replica sets or sharded clusters, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-x509-member-authentication/">
Use x.509 Certificate for Membership Authentication with Self-Managed MongoDB
<!-- -->
.
</a>
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Prerequisites
<a class="headerlink leafygreen-ui-1kepx47" href="#prerequisites" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="prerequisites">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
A full description of TLS/SSL, PKI (Public Key Infrastructure) certificates, in particular x.509 certificates, and Certificate Authority is beyond the scope of this document.
This tutorial assumes prior knowledge of TLS/SSL as well as access to valid x.509 certificates.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Certificate Authority
<a class="headerlink leafygreen-ui-1kepx47" href="#certificate-authority" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="certificate-authority">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For production use, your MongoDB deployment should use valid
certificates generated and signed by a certificate authority. You or
your organization can generate and maintain an independent certificate
authority, or use certificates generated by third-party TLS vendors.
Obtaining and managing certificates is beyond the scope of this
documentation.
</p>
<p class="leafygreen-ui-1kp3ins">
To use x.509 authentication,
<code class="leafygreen-ui-1l06pbn">
--tlsCAFile
</code>
or
<code class="leafygreen-ui-1l06pbn">
net.tls.CAFile
</code>
must be specified unless you are using
<code class="leafygreen-ui-1l06pbn">
--tlsCertificateSelector
</code>
or
<code class="leafygreen-ui-1l06pbn">
--net.tls.certificateSelector
</code>
.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Client x.509 Certificate
<a class="headerlink leafygreen-ui-1kepx47" href="#client-x.509-certificate" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="client-x.509-certificate">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You must have valid x.509 certificates. The client x.509 certificates
must meet the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-x.509/#std-label-client-x509-certificates-requirements">
client certificate requirements
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If you specify
<code class="leafygreen-ui-1l06pbn">
--tlsAllowInvalidCertificates
</code>
or
<code class="leafygreen-ui-1l06pbn">
net.tls.allowInvalidCertificates: true
</code>
, an invalid certificate is
sufficient only to establish a TLS connection but it is
<em>
insufficient
</em>
for authentication.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedure
<a class="headerlink leafygreen-ui-1kepx47" href="#procedure" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedure">
</div>
</a>
</h2>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Deploy with x.509 AuthenticationYou can configure a mongod instance for x.509\nauthentication from the command-line.To configure a standalone mongod instance, run\nthe following command:Include additional options as required for your configuration.The x.509 configuration requires:OptionNotes--tlsModeSpecify requireTLS.--tlsCertificateKeyFileSpecify the instance's x.509 certificate to present to\nclients.--tlsCAFileSpecify the Certificate Authority file to verify the\ncertificates presented to the instance.You can configure a mongod for x.509\nauthentication in the configuration file.To configure a standalone mongod instance, add\nthe following configuration options to your configuration\nfile:Include additional options as required for your configuration.The x.509 configuration requires:OptionNotesnet.tls.modeSpecify requireTLS.net.tls.certificateKeyFileSpecify the instance's x.509 certificate to present to\nclients.net.tls.CAFileSpecify the Certificate Authority file to verify the\ncertificates presented to the instance.To set up x.509 authentication for replica sets or sharded clusters,\nsee Use x.509 Certificate for Membership Authentication with Self-Managed MongoDB."},{"@type":"HowToStep","text":"Add x.509 Certificate subject as a UserTo authenticate with a client certificate, you must first add the value\nof the subject from the client certificate as a MongoDB user to the\n$external database. Each unique x.509 client certificate\ncorresponds to a single MongoDB user. You cannot use a single client\ncertificate to authenticate more than one MongoDB user.To use Client Sessions and Causal Consistency Guarantees with $external authentication users\n(Kerberos, LDAP, or x.509 users), usernames cannot be greater\nthan 10k bytes.The RDNs in the subject string must be compatible with the\nRFC2253 standard.You can retrieve the RFC2253 formatted subject from the\nclient certificate with the following command:The command returns the subject string and the certificate:Add the RFC2253 compliant value of the subject as a user.\nOmit spaces as needed.The following example adds a user and grants the user\nreadWrite role in the test database and the\nuserAdminAnyDatabase role:See Manage Users and Roles on Self-Managed Deployments for details on adding a user\nwith roles."},{"@type":"HowToStep","text":"Authenticate with a x.509 CertificateAfter you have added the x.509 client certificate subject as a\ncorresponding MongoDB user, you can\nauthenticate with the client certificate:To authenticate during connection, run the following command:OptionNotes--tls--tlsCertificateKeyFileSpecify the client's x.509 file.--tlsCAFileSpecify the Certificate Authority file to verify the\ncertificate presented by the mongod\ninstance.--authenticationDatabaseSpecify '$external'.--authenticationMechanismSpecify MONGODB-X509.You can connect without authentication and use the\ndb.auth() method to authenticate after\nconnection.For example, if using mongosh,Connect mongosh to the\nmongod:OptionNotes--tls--tlsCertificateKeyFileSpecify the client's x.509 file.--tlsCAFileSpecify the Certificate Authority file to verify\nthe certificate presented by the\nmongod or mongos\ninstance.To authenticate, use the db.auth() method in\nthe $external database. For the mechanism field,\nspecify \"MONGODB-X509\"."}],"name":"Procedure","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-4nyere eccljxs0">
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
1
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Deploy with x.509 Authentication
<a class="headerlink leafygreen-ui-1kepx47" href="#deploy-with-x-509-authentication" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="deploy-with-x-509-authentication">
</div>
</a>
</h3>
<span class="leafygreen-ui-1qnjd6d" id="std-label-x509-deploy-tls">
</span>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-9">
<div aria-label="Tabs to describe usage of cfg/cmdline" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_gw4g7mb6v" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_jr25nosoe" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Command Options
</div>
</button>
<button aria-controls="_pd31nbpy4" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_5fdg4hdrn" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Configuration File
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_vkq9zn6g9" class="" id="_3hw15m2nu" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can configure a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance for x.509
authentication from the command-line.
</p>
<p class="leafygreen-ui-1kp3ins">
To configure a standalone
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance, run
the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --tlsMode requireTLS \\\n --tlsCertificateKeyFile <path> \\\n --tlsCAFile <path> --bind_ip <hostnames>","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongod --tlsMode requireTLS \</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> --tlsCertificateKeyFile <path to TLS/SSL certificate and key PEM file> \</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> --tlsCAFile <path to root CA PEM file> --bind_ip <hostnames></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Include additional options as required for your configuration.
</p>
<p class="leafygreen-ui-1kp3ins">
The x.509 configuration requires:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:75%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Option
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Notes
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--tlsMode">
<code class="leafygreen-ui-1l06pbn">
--tlsMode
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify
<code class="leafygreen-ui-1l06pbn">
requireTLS
</code>
.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--tlsCertificateKeyFile">
<code class="leafygreen-ui-1l06pbn">
--tlsCertificateKeyFile
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the instance's x.509 certificate to present to
clients.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--tlsCAFile">
<code class="leafygreen-ui-1l06pbn">
--tlsCAFile
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the Certificate Authority file to verify the
certificates presented to the instance.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_3g8ja61hq" class="leafygreen-ui-38lglc" id="_u8mhgekz2" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can configure a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
for x.509
authentication in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/">
configuration file
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To configure a standalone
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance, add
the following configuration options to your configuration
file:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"net:\n tls:\n mode: requireTLS\n certificateKeyFile: <path>\n CAFile: <path>","programmingLanguage":"YAML"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light yaml leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tls:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">mode:</span> <span class="lg-highlight-string">requireTLS</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">certificateKeyFile:</span> <span class="lg-highlight-string"><path</span> <span class="lg-highlight-string">to</span> <span class="lg-highlight-string">TLS/SSL</span> <span class="lg-highlight-string">certificate</span> <span class="lg-highlight-string">and</span> <span class="lg-highlight-string">key</span> <span class="lg-highlight-string">PEM</span> <span class="lg-highlight-string">file></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">CAFile:</span> <span class="lg-highlight-string"><path</span> <span class="lg-highlight-string">to</span> <span class="lg-highlight-string">root</span> <span class="lg-highlight-string">CA</span> <span class="lg-highlight-string">PEM</span> <span class="lg-highlight-string">file></span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Include additional options as required for your configuration.
</p>
<p class="leafygreen-ui-1kp3ins">
The x.509 configuration requires:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:75%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Option
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Notes
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.mode">
<code class="leafygreen-ui-1l06pbn">
net.tls.mode
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify
<code class="leafygreen-ui-1l06pbn">
requireTLS
</code>
.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.certificateKeyFile">
<code class="leafygreen-ui-1l06pbn">
net.tls.certificateKeyFile
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the instance's x.509 certificate to present to
clients.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.CAFile">
<code class="leafygreen-ui-1l06pbn">
net.tls.CAFile
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the Certificate Authority file to verify the
certificates presented to the instance.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To set up x.509 authentication for replica sets or sharded clusters,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-x509-member-authentication/#std-label-x509-internal-authentication">
Use x.509 Certificate for Membership Authentication with Self-Managed MongoDB
<!-- -->
.
</a>
</p>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
2
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Add x.509 Certificate
<code class="leafygreen-ui-1l06pbn">
subject
</code>
as a User
<a class="headerlink leafygreen-ui-1kepx47" href="#add-x-509-certificate-subject-as-a-user" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="add-x-509-certificate-subject-as-a-user">
</div>
</a>
</h3>
<span class="leafygreen-ui-1qnjd6d" id="std-label-addX509SubjectUser">
</span>
<p class="leafygreen-ui-1kp3ins">
To authenticate with a client certificate, you must first add the value
of the
<code class="leafygreen-ui-1l06pbn">
subject
</code>
from the client certificate as a MongoDB user to the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database. Each unique x.509 client certificate
corresponds to a single MongoDB user. You cannot use a single client
certificate to authenticate more than one MongoDB user.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Username Requirements
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-isolation-consistency-recency/#std-label-sessions">
Client Sessions and Causal Consistency Guarantees
</a>
with
<code class="leafygreen-ui-1l06pbn">
$external
</code>
authentication users
(Kerberos, LDAP, or x.509 users), usernames cannot be greater
than 10k bytes.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The RDNs in the
<code class="leafygreen-ui-1l06pbn">
subject
</code>
string must be compatible with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.ietf.org/rfc/rfc2253.txt" rel="noopener noreferrer">
<span>
RFC2253
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
standard.
</p>
</li>
</ul>
</div>
</div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You can retrieve the
<code class="leafygreen-ui-1l06pbn">
RFC2253
</code>
formatted
<code class="leafygreen-ui-1l06pbn">
subject
</code>
from the
client certificate with the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"openssl x509 -in <pathtoclientpem> -inform PEM -subject -nameopt RFC2253","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">openssl x509 -<span class="lg-highlight-keyword">in</span> <pathToClientPEM> -inform PEM -subject -nameopt RFC2253</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The command returns the
<code class="leafygreen-ui-1l06pbn">
subject
</code>
string and the certificate:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"subject= CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry\n-----BEGIN CERTIFICATE-----\n# ...\n-----END CERTIFICATE-----","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">subject= CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry</td></tr><tr class=""><td class="leafygreen-ui-7razhx">-----BEGIN CERTIFICATE-----</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"># ...</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">-----END CERTIFICATE-----</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Add the
<code class="leafygreen-ui-1l06pbn">
RFC2253
</code>
compliant value of the
<code class="leafygreen-ui-1l06pbn">
subject
</code>
as a user.
Omit spaces as needed.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example adds a user and grants the user
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-readWrite">
<code class="leafygreen-ui-1l06pbn">
readWrite
</code>
</a>
role in the
<code class="leafygreen-ui-1l06pbn">
test
</code>
database and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-userAdminAnyDatabase">
<code class="leafygreen-ui-1l06pbn">
userAdminAnyDatabase
</code>
</a>
role:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"$external\").runCommand(\n {\n createUser: \"CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry\",\n roles: [\n { role: \"readWrite\", db: \"test\" },\n { role: \"userAdminAnyDatabase\", db: \"admin\" }\n ],\n writeConcern: { w: \"majority\" , wtimeout: 5000 }\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"$external"</span>).<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">createUser</span>: <span class="lg-highlight-string">"CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">roles</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"readWrite"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"test"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"userAdminAnyDatabase"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"admin"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">writeConcern</span>: <!-- -->{ <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-string">"majority"</span> <!-- -->, <span class="lg-highlight-attr">wtimeout</span>: <span class="lg-highlight-number">5000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-users-and-roles/">
Manage Users and Roles on Self-Managed Deployments
</a>
for details on adding a user
with roles.
</p>
</li>
</ol>
</section>
</div>
</div>
<div class="css-1nuklj8 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-19lsqsq e1ckbxcu2">
3
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Authenticate with a x.509 Certificate
<a class="headerlink leafygreen-ui-1kepx47" href="#authenticate-with-a-x-509-certificate" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="authenticate-with-a-x-509-certificate">
</div>
</a>
</h3>
<span class="leafygreen-ui-1qnjd6d" id="std-label-authenticate-with-x509-cert">
</span>
<p class="leafygreen-ui-1kp3ins">
After you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-addX509SubjectUser" target="_self">
<span>
added the x.509 client certificate subject as a
corresponding MongoDB user
</span>
</a>
, you can
authenticate with the client certificate:
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-12">
<div aria-label="Tabs to describe usage of authafter/connect" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_4plg5l25p" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_myqky9kbz" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Connect with Authentication
</div>
</button>
<button aria-controls="_ch69gr2i6" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_i1e382voq" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Authenticate after Connection
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_e0407lm0e" class="" id="_v7mr1kzmt" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To authenticate during connection, run the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --tls --tlsCertificateKeyFile <path> \\\n --tlsCAFile <path> \\\n --authenticationDatabase '$external' \\\n --authenticationMechanism MONGODB-X509","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongosh --tls --tlsCertificateKeyFile <path to client PEM file> \</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> --tlsCAFile <path to root CA PEM file> \</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> --authenticationDatabase <span class="lg-highlight-string">'$external'</span> \</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> --authenticationMechanism MONGODB-X509</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:70%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Option
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Notes
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--tls" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--tls
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--tlsCertificateKeyFile" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--tlsCertificateKeyFile
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the client's x.509 file.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--tlsCAFile" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--tlsCAFile
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the Certificate Authority file to verify the
certificate presented by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instance.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--authenticationDatabase" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--authenticationDatabase
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify
<code class="leafygreen-ui-1l06pbn">
'$external'
</code>
.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--authenticationMechanism" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--authenticationMechanism
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify
<code class="leafygreen-ui-1l06pbn">
MONGODB-X509
</code>
.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_03fgdpr4h" class="leafygreen-ui-38lglc" id="_p8d5rqd98" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can connect without authentication and use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.auth/#mongodb-method-db.auth">
<code class="leafygreen-ui-1l06pbn">
db.auth()
</code>
</a>
method to authenticate after
connection.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, if using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
,
</span>
</a>
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --tls --tlsCertificateKeyFile <path> \\\n --tlsCAFile <path>","programmingLanguage":"Bash"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongosh --tls --tlsCertificateKeyFile <path to client PEM file> \</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> --tlsCAFile <path to root CA PEM file></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:25%"/>
<col style="width:75%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Option
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Notes
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--tls" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--tls
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--tlsCertificateKeyFile" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--tlsCertificateKeyFile
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the client's x.509 file.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/options/#std-option-mongosh.--tlsCAFile" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
--tlsCAFile
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the Certificate Authority file to verify
the certificate presented by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To authenticate, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.auth/#mongodb-method-db.auth">
<code class="leafygreen-ui-1l06pbn">
db.auth()
</code>
</a>
method in
the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database. For the
<code class="leafygreen-ui-1l06pbn">
mechanism
</code>
field,
specify
<code class="leafygreen-ui-1l06pbn">
"MONGODB-X509"
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"$external\").auth(\n {\n mechanism: \"MONGODB-X509\"\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"$external"</span>).<span class="lg-highlight-title lg-highlight-function">auth</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">mechanism</span>: <span class="lg-highlight-string">"MONGODB-X509"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Next Steps
<a class="headerlink leafygreen-ui-1kepx47" href="#next-steps" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="next-steps">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To use x.509 authentication for replica sets or sharded clusters, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-x509-member-authentication/">
Use x.509 Certificate for Membership Authentication with Self-Managed MongoDB
<!-- -->
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/changeStreams/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Change Streams
<a class="headerlink leafygreen-ui-1kepx47" href="#change-streams" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="change-streams">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#availability" target="_self">
<span>
Availability
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#connect" target="_self">
<span>
Connect
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#watch-a-collection--database--or-deployment" target="_self">
<span>
Watch a Collection, Database, or Deployment
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#change-stream-performance-considerations" target="_self">
<span>
Change Stream Performance Considerations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#open-a-change-stream" target="_self">
<span>
Open A Change Stream
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#modify-change-stream-output" target="_self">
<span>
Modify Change Stream Output
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#lookup-full-document-for-update-operations" target="_self">
<span>
Lookup Full Document for Update Operations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#resume-a-change-stream" target="_self">
<span>
Resume a Change Stream
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#use-cases" target="_self">
<span>
Use Cases
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#access-control" target="_self">
<span>
Access Control
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#event-notification" target="_self">
<span>
Event Notification
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#collation" target="_self">
<span>
Collation
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#change-streams-and-orphan-documents" target="_self">
<span>
Change Streams and Orphan Documents
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#change-streams-with-document-pre--and-post-images" target="_self">
<span>
Change Streams with Document Pre- and Post-Images
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Change streams allow applications to access real-time data changes
without the prior complexity and risk of manually tailing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
.
Applications can use change streams to subscribe to all data changes on
a single collection, a database, or an entire deployment, and
immediately react to them. Because change streams use the aggregation
framework, applications can also filter for specific changes or
transform the notifications at will.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.1,
<!-- -->
change streams
<!-- -->
are optimized, providing more
efficient resource utilization and faster execution of some aggregation
pipeline stages.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Availability
<a class="headerlink leafygreen-ui-1kepx47" href="#availability" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="availability">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Change streams are available for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/replication/#std-label-replication">
replica sets
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharding-background">
sharded clusters
<!-- -->
:
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<strong>
Storage Engine.
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
The replica sets and sharded clusters must use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. Change streams can also be used
on deployments that employ MongoDB's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-encryption-at-rest/#std-label-encrypted-storage-engine">
encryption-at-rest
</a>
feature.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<strong>
Replica Set Protocol Version.
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
The replica sets and sharded clusters must use replica set protocol
version 1 (
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.protocolVersion">
<code class="leafygreen-ui-1l06pbn">
pv1
</code>
</a>
).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<strong>
Read Concern "majority" Enablement.
</strong>
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-changeStreams" target="_self">
<span>
Change streams
</span>
</a>
are
available regardless of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
read concern
support; that is, read concern
<code class="leafygreen-ui-1l06pbn">
majority
</code>
support can be either
enabled (default) or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#std-label-disable-read-concern-majority">
disabled
</a>
to use change streams.
</p>
</li>
</ul>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stable API Support
<a class="headerlink leafygreen-ui-1kepx47" href="#stable-api-support" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stable-api-support">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Change streams are included in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/stable-api/#std-label-stable-api">
Stable API
</a>
V1.
However, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-streams-expanded-events">
showExpandedEvents
</a>
option is not included in Stable API V1.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Connect
<a class="headerlink leafygreen-ui-1kepx47" href="#connect" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="connect">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Connections for a change stream can either use DNS seed lists
with the
<code class="leafygreen-ui-1l06pbn">
+srv
</code>
connection option or by listing the servers individually
in the connection string.
</p>
<p class="leafygreen-ui-1kp3ins">
If the driver loses the connection to a change stream or the connection goes
down, it attempts to reestablish a connection to the change stream through
another node in the cluster that has a matching
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/connection-string-options/#std-label-connections-read-preference">
read preference
</a>
. If the driver cannot find
a node with the correct read preference, it throws an exception.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/connection-string/#std-label-mongodb-uri">
Connection String URI Format
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-changeStreams-watch-deployment">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Watch a Collection, Database, or Deployment
<a class="headerlink leafygreen-ui-1kepx47" href="#watch-a-collection--database--or-deployment" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="watch-a-collection--database--or-deployment">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can open change streams against:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:15%"/>
<col style="width:85%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Target
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A collection
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
You can open a change stream cursor for a single collection
(except
<code class="leafygreen-ui-1l06pbn">
system
</code>
collections, or any collections in the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
,
<code class="leafygreen-ui-1l06pbn">
local
</code>
, and
<code class="leafygreen-ui-1l06pbn">
config
</code>
databases).
</p>
<p class="leafygreen-ui-1kp3ins">
The examples on this page use the MongoDB drivers to open and
work with a change stream cursor for a single collection. See
also the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
db.collection.watch()
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A database
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
You can open a change stream cursor for a single database (excluding
<code class="leafygreen-ui-1l06pbn">
admin
</code>
,
<code class="leafygreen-ui-1l06pbn">
local
</code>
, and
<code class="leafygreen-ui-1l06pbn">
config
</code>
database) to watch for changes to
all its non-system collections.
</p>
<p class="leafygreen-ui-1kp3ins">
For the MongoDB driver method, refer to your driver
documentation. See also the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.watch/#mongodb-method-db.watch">
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A deployment
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
You can open a change stream cursor for a deployment (either a replica
set or a sharded cluster) to watch for changes to all non-system
collections across all databases except for
<code class="leafygreen-ui-1l06pbn">
admin
</code>
,
<code class="leafygreen-ui-1l06pbn">
local
</code>
, and
<code class="leafygreen-ui-1l06pbn">
config
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For the MongoDB driver method, refer to your driver
documentation. See also the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo.watch/#mongodb-method-Mongo.watch">
<code class="leafygreen-ui-1l06pbn">
Mongo.watch()
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Change Stream Examples
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The examples on this page use the MongoDB drivers to illustrate how
to open a change stream cursor for a collection and work with the
change stream cursor.
</p>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Change Stream Performance Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#change-stream-performance-considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="change-stream-performance-considerations">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If the amount of active change streams opened against a database exceeds the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/connection-pool-overview/#std-label-connection-pool-overview">
connection pool size
</a>
, you may
experience notification latency. Each change stream uses a connection
and a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#std-label-manual-reference-commands-getMore">
getMore
</a>
operation on the change stream for the period of time that it waits for the next event.
To avoid any latency issues, you should ensure that the pool size is greater than the
number of opened change streams. For details see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/connection-pool-overview/#std-label-maxpoolsize-cp-setting">
maxPoolSize
</a>
setting.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sharded Cluster Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#sharded-cluster-considerations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sharded-cluster-considerations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When a change stream is opened on a sharded cluster:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
creates individual change streams on
<strong>
each
shard
</strong>
. This behavior occurs regardless of whether the change stream
targets a particular shard key range.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When the
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
receives change stream results, it sorts and
filters those results. If needed, the
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
also performs a
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
lookup.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For best performance, limit the use of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
queries in
change streams.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-open-change-stream">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Open A Change Stream
<a class="headerlink leafygreen-ui-1kepx47" href="#open-a-change-stream" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="open-a-change-stream">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To open a change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a replica set, you can issue the open change stream operation
from any of the data-bearing members.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a sharded cluster, you must issue the open change stream
operation from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The following example opens a change stream for a collection and
iterates over the cursor to retrieve the change stream documents.
<a class="footnote-reference header-buffer" href="#footnote-start-time" id="ref-start-time-id1">
[
<!-- -->
1
<!-- -->
]
</a>
</p>
<hr class="css-1unva0n e4ru7ww0"/>
<p class="leafygreen-ui-1kp3ins">
➤
<!-- -->
Use the
<strong>
Select your language
</strong>
drop-down menu in the
upper-right to set the language of the examples on this page.
</p>
<hr class="css-1unva0n e4ru7ww0"/>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-f2qztv" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-11">
<div aria-label="Tabs to describe usage of drivers" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_rwzl22vdg" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_j4pwxi58n" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C
</div>
</button>
<button aria-controls="_019t51634" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_kjjpl7s4z" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C#
</div>
</button>
<button aria-controls="_b4ifci05z" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_v5n6alkof" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Go
</div>
</button>
<button aria-controls="_u4xhlcfum" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_v08pk39zd" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Java (Sync)
</div>
</button>
<button aria-controls="_xb3b8laoi" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_42j4et8it" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Kotlin (Coroutine)
</div>
</button>
<button aria-controls="_h9ywyc7mx" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_yicdesurd" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Motor
</div>
</button>
<button aria-controls="_6diebken9" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_j6hdz3s37" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Node.js
</div>
</button>
<button aria-controls="_bnvpyflf1" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_tx3iygnev" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
PHP
</div>
</button>
<button aria-controls="_hf41o69m5" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_nuicwebo6" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Python
</div>
</button>
<button aria-controls="_53myn0aut" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_gurebt85j" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Ruby
</div>
</button>
<button aria-controls="_7eip297mk" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_5nemx19u9" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Async)
</div>
</button>
<button aria-controls="_nzm8x542b" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_np133aywn" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Sync)
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_wj5jvj4zy" class="leafygreen-ui-38lglc" id="_wf59j4ear" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The C examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://mongoc.org/libmongoc/current/tutorial.html#making-a-connection/" rel="noopener noreferrer">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongoc_collection_t *collection;\nbson_t *pipeline = bson_new ();\nbson_t opts = BSON_INITIALIZER;\nmongoc_change_stream_t *stream;\nconst bson_t *change;\nconst bson_t *resume_token;\nbson_error_t error;\n\ncollection = mongoc_database_get_collection (db, \"inventory\");\nstream = mongoc_collection_watch (collection, pipeline, NULL /* opts */);\nmongoc_change_stream_next (stream, &change);\nif (mongoc_change_stream_error_document (stream, &error, NULL)) {\n MONGOC_ERROR (\"%s\\n\", error.message);\n}\n\nmongoc_change_stream_destroy (stream);","programmingLanguage":"C"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">mongoc_collection_t *collection;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bson_t *<!-- -->pipeline <!-- -->= <!-- -->bson_new ();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bson_t opts = <!-- --><span class="lg-highlight-variable lg-highlight-constant">BSON_INITIALIZER</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">mongoc_change_stream_t *stream;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> bson_t *change;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> bson_t *resume_token;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">bson_error_t error;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">collection <!-- -->= <!-- -->mongoc_database_get_collection (db<!-- -->, <span class="lg-highlight-string">"inventory"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">stream <!-- -->= <!-- -->mongoc_collection_watch (collection, pipeline<!-- -->, <span class="lg-highlight-variable lg-highlight-constant">NULL</span> <span class="lg-highlight-comment">/* opts */</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">mongoc_change_stream_next <!-- -->(<!-- -->stream, &change);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">if</span> <!-- -->(<!-- -->mongoc_change_stream_error_document (stream<!-- -->, <!-- -->&error, <span class="lg-highlight-variable lg-highlight-constant">NULL</span>)) {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><span class="lg-highlight-variable lg-highlight-constant">MONGOC_ERROR</span> (<span class="lg-highlight-string">"%s</span><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.<span class="lg-highlight-property">message</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">mongoc_change_stream_destroy <!-- -->(<!-- -->stream);</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-30-menu" aria-describedby="select-30-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-31" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4m:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4m:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4m:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4m:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_mx516vmru" class="leafygreen-ui-38lglc" id="_lhs5fclmv" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The C# examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://mongodb.github.io/mongo-csharp-driver/2.4/getting_started/quick_tour/#make-a-connection/" rel="noopener noreferrer">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var cursor = inventory.Watch();\nwhile (cursor.MoveNext() && cursor.Current.Count() == 0) { } // keep calling MoveNext until we've read the first batch\nvar next = cursor.Current.First();\ncursor.Dispose();","programmingLanguage":"C#"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> cursor = inventory.<span class="lg-highlight-title lg-highlight-class">Watch</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">while</span> (cursor.<span class="lg-highlight-title lg-highlight-class">MoveNext</span>() && cursor.<span class="lg-highlight-property">Current</span>.<span class="lg-highlight-title lg-highlight-class">Count</span>() == <span class="lg-highlight-number">0</span>) { } <span class="lg-highlight-comment">// keep calling MoveNext</span><span class="lg-highlight-comment"> until we've read </span><span class="lg-highlight-comment">the first batch</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> next = cursor.<span class="lg-highlight-property">Current</span>.<span class="lg-highlight-title lg-highlight-class">First</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">cursor.<span class="lg-highlight-title lg-highlight-class">Dispose</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-33-menu" aria-describedby="select-33-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-34" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4n:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4n:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4n:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4n:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_gy442hl3x" class="leafygreen-ui-38lglc" id="_07pkrqj5y" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The Go examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://godoc.org/go.mongodb.org/mongo-driver/mongo#NewClient/" rel="noopener noreferrer">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\ncs, err := coll.Watch(ctx, mongo.Pipeline{})\nassert.NoError(t, err)\ndefer cs.Close(ctx)\n\nok := cs.Next(ctx)\nnext := cs.Current\n","programmingLanguage":"Golang"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cs<!-- -->, <!-- -->err := coll.<span class="lg-highlight-title lg-highlight-class">Watch</span>(ctx, mongo.<span class="lg-highlight-property">Pipeline</span>{})</td></tr><tr class=""><td class="leafygreen-ui-7razhx">assert.<span class="lg-highlight-title lg-highlight-class">NoError</span>(t, err)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">defer cs.<span class="lg-highlight-title lg-highlight-class">Close</span>(ctx)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">ok <!-- -->:<!-- -->= cs.<span class="lg-highlight-title lg-highlight-class">Next</span>(ctx)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">next <!-- -->:<!-- -->= cs.<span class="lg-highlight-property">Current</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-36-menu" aria-describedby="select-36-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-37" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4o:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4o:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4o:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4o:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_pxof26o19" class="leafygreen-ui-38lglc" id="_0uy8ke496" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The Java examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://mongodb.github.io/mongo-java-driver/3.6/driver/tutorials/databases-collections/" rel="noopener noreferrer">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"MongoCursor<changestreamdocument<document>> cursor = inventory.watch().iterator();\nChangeStreamDocument<document> next = cursor.next();","programmingLanguage":"Java"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-class">MongoCursor</span><<span class="lg-highlight-title lg-highlight-class">ChangeStreamDocument</span><<span class="lg-highlight-title lg-highlight-class">Document</span>>> cursor = inventory.<span class="lg-highlight-title lg-highlight-function">watch</span>().<span class="lg-highlight-title lg-highlight-function">iterator</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-class">ChangeStreamDocument</span><<span class="lg-highlight-title lg-highlight-class">Document</span>> next = cursor.<span class="lg-highlight-title lg-highlight-function">next</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-39-menu" aria-describedby="select-39-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-40" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4p:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4p:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4p:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4p:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_6bryj5i90" class="leafygreen-ui-38lglc" id="_mtd858ij3" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The Kotlin examples below assume that you are connected to a MongoDB replica set and can access a database
that contains the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection. To learn more about completing these tasks, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/kotlin/coroutine/current/fundamentals/databases-collections/" target="_self">
<span>
Kotlin Coroutine Driver Databases and Collections
</span>
</a>
guide.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"val job = launch {\n val changeStream = collection.watch()\n changeStream.collect {\n println(\"Received a change event: $it\")\n }\n}","programmingLanguage":"Kotlin"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">val job = launch {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> val changeStream = collection.<span class="lg-highlight-title lg-highlight-function">watch</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<span class="lg-highlight-property">collect</span> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">println</span>(<span class="lg-highlight-string">"Received a change event: $it"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-42-menu" aria-describedby="select-42-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-43" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4q:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4q:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4q:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4q:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_7ynh53z7d" class="leafygreen-ui-38lglc" id="_wftihjni6" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://motor.readthedocs.io/en/stable/tutorial-asyncio.html#creating-a-client" rel="noopener noreferrer">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cursor = db.inventory.watch()\ndocument = await cursor.next()","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cursor = db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">watch</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-variable lg-highlight-language">document</span> = <span class="lg-highlight-keyword">await</span> cursor.<span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-45-menu" aria-describedby="select-45-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-46" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4r:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4r:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4r:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4r:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_c1n0wxgh0" class="" id="_j2pixna6a" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The Node.js examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#connect" rel="noopener noreferrer">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example uses stream to process the change events.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const collection = db.collection('inventory');\nconst changeStream = collection.watch();\nchangeStream.on('change', next => {\n // process next document\n});","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> collection <!-- -->= <!-- -->db.<span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">'inventory'</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> changeStream <!-- -->= <!-- -->collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">changeStream.<span class="lg-highlight-title lg-highlight-function">on</span>(<span class="lg-highlight-string">'change'</span>, <span class="lg-highlight-params lg-highlight-function">next</span><span class="lg-highlight-function"> =></span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// process next document</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">})<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-48-menu" aria-describedby="select-48-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-49" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4s:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4s:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4s:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4s:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Alternatively, you can also use iterator to process the change events:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const collection = db.collection('inventory');\nconst changeStream = collection.watch();\nconst next = await changeStream.next();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> collection <!-- -->= <!-- -->db.<span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">'inventory'</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> changeStream <!-- -->= <!-- -->collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> next <!-- -->= <span class="lg-highlight-keyword">await</span> changeStream.<span class="lg-highlight-title lg-highlight-function">next</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-51-menu" aria-describedby="select-51-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-52" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4t:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4t:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4t:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4t:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
ChangeStream extends
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mongodb.github.io/node-mongodb-native/5.7/classes/TypedEventEmitter.html" rel="noopener noreferrer">
<span>
EventEmitter
<!-- -->
.
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_lh419s4g6" class="leafygreen-ui-38lglc" id="_u9mco72vg" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/php-library/current/reference/method/MongoDBClient__construct/" target="_self">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"$changeStream = $db->inventory->watch();\n$changeStream->rewind();\n\n$firstChange = $changeStream->current();\n\n$changeStream->next();\n\n$secondChange = $changeStream->current();","programmingLanguage":"PHP"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">$changeStream = $db-><!-- -->inventory-><span class="lg-highlight-title lg-highlight-function">watch</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream-<!-- -->><span class="lg-highlight-title lg-highlight-function">rewind</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$firstChange = $changeStream-><!-- --><span class="lg-highlight-title lg-highlight-function">current</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream-<!-- -->><span class="lg-highlight-title lg-highlight-function">next</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$secondChange = $changeStream-><!-- --><span class="lg-highlight-title lg-highlight-function">current</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-54-menu" aria-describedby="select-54-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-55" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4u:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4u:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4u:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4u:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_5zjgn4zvu" class="leafygreen-ui-38lglc" id="_0yi03hxbn" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The Python examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/pymongo/" target="_self">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cursor = db.inventory.watch()\nnext(cursor)","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cursor = db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">watch</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-function">next</span>(cursor)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-57-menu" aria-describedby="select-57-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-58" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r4v:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r4v:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4v:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r4v:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_0vfpai7tl" class="leafygreen-ui-38lglc" id="_4qrrlx9xm" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/ruby-driver/current/reference/create-client/" target="_self">
<span>
connected to a MongoDB replica set and have accessed a database
</span>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\ncursor = inventory.watch.to_enum\nnext_change = cursor.next\n","programmingLanguage":"Ruby"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cursor <!-- -->= <!-- -->inventory.<span class="lg-highlight-property">watch</span>.<span class="lg-highlight-property">to_enum</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">next_change <!-- -->= <!-- -->cursor.<span class="lg-highlight-property">next</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-60-menu" aria-describedby="select-60-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-61" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r50:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r50:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r50:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r50:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_c4r43z8dn" class="leafygreen-ui-38lglc" id="_opvj9httb" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The Swift (Async) examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mongodb.github.io/mongo-swift-driver/docs/current/MongoSwift/Classes/MongoClient.html" rel="noopener noreferrer">
<span>
connected to a MongoDB replica set and have accessed a
database
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let inventory = db.collection(\"inventory\")\n\n// Option 1: retrieve next document via next()\nlet next = inventory.watch().flatMap { cursor in\n cursor.next()\n}\n\n// Option 2: register a callback to execute for each document\nlet result = inventory.watch().flatMap { cursor in\n cursor.forEach { event in\n // process event\n print(event)\n }\n}","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> inventory = db.<!-- --><span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">"inventory"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Option 1:</span><span class="lg-highlight-comment"> retrieve next document </span><span class="lg-highlight-comment">via next()</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> next = inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>().<span class="lg-highlight-property">flatMap</span> { cursor <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor.<!-- --><span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Option 2:</span><span class="lg-highlight-comment"> register a callback </span><span class="lg-highlight-comment">to</span><span class="lg-highlight-comment"> execute for each </span><span class="lg-highlight-comment">document</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> result = inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>().<span class="lg-highlight-property">flatMap</span> { cursor <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor.<!-- --><!-- --><span class="lg-highlight-property">forEach</span> { event <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// process event</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(event)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-63-menu" aria-describedby="select-63-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-64" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r51:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r51:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r51:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r51:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_mmdnl7usm" class="leafygreen-ui-38lglc" id="_yxlmrh3sw" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
The Swift (Sync) examples below assume that you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mongodb.github.io/mongo-swift-driver/docs/current/MongoSwiftSync/Classes/MongoClient.html" rel="noopener noreferrer">
<span>
connected to a MongoDB replica set and have accessed a
database
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
that contains an
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let inventory = db.collection(\"inventory\")\nlet changeStream = try inventory.watch()\nlet next = changeStream.next()","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> inventory = db.<!-- --><span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">"inventory"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> changeStream <!-- -->= <span class="lg-highlight-keyword">try</span> inventory.<span class="lg-highlight-title lg-highlight-function">watch</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> next = changeStream.<!-- --><span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-66-menu" aria-describedby="select-66-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-67" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r52:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r52:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r52:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r52:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To retrieve the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
data change event
</a>
from
the cursor, iterate the change stream cursor. For information on the
change stream event, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
Change Events
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-changeStreams" target="_self">
<span>
change stream cursor
</span>
</a>
remains open until
one of the following occurs:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The cursor is explicitly closed.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/invalidate/#std-label-change-event-invalidate">
invalidate event
</a>
occurs; for
example, a collection drop or rename.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The connection to the MongoDB deployment closes or times out.
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/iterate-a-cursor/#std-label-cursor-behaviors">
Cursor Behaviors
</a>
for more information.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the deployment is a sharded cluster, a shard removal may cause an
open change stream cursor to close. The closed change stream cursor
may not be fully resumable.
</p>
</li>
</ul>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The lifecycle of an unclosed cursor is language-dependent.
</p>
</div>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-start-time" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-start-time-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
You can specify a
<code class="leafygreen-ui-1l06pbn">
startAtOperationTime
</code>
to open the cursor at a particular
point in time. If the specified starting point is in the past, it must be in
the time range of the oplog.
</td>
</tr>
</tbody>
</table>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-stream-modify-output">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Modify Change Stream Output
<a class="headerlink leafygreen-ui-1kepx47" href="#modify-change-stream-output" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="modify-change-stream-output">
</div>
</a>
</h2>
<hr class="css-1unva0n e4ru7ww0"/>
<p class="leafygreen-ui-1kp3ins">
➤
<!-- -->
Use the
<strong>
Select your language
</strong>
drop-down menu in the
upper-right to set the language of the examples on this page.
</p>
<hr class="css-1unva0n e4ru7ww0"/>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-f2qztv" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-13">
<div aria-label="Tabs to describe usage of drivers" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_zd64wm3lu" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_cnevp9kpp" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C
</div>
</button>
<button aria-controls="_avuf0p12g" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_upmvfod84" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C#
</div>
</button>
<button aria-controls="_pghtgvynz" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_cxx6mj09e" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Go
</div>
</button>
<button aria-controls="_x0y4c0145" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_dftpsi2f2" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Java (Sync)
</div>
</button>
<button aria-controls="_tpbdrcu2z" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_0nvowu64b" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Kotlin (Coroutine)
</div>
</button>
<button aria-controls="_l30q0k8c3" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_s2e5n57ig" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Motor
</div>
</button>
<button aria-controls="_u1021nci6" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_fm76bf4mi" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Node.js
</div>
</button>
<button aria-controls="_fohvs0qj8" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_tuoakvyzb" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
PHP
</div>
</button>
<button aria-controls="_30c8at61i" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_56flu8fw6" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Python
</div>
</button>
<button aria-controls="_26g3rdpyj" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_ae0onq8qa" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Ruby
</div>
</button>
<button aria-controls="_hdoj93gbv" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_mfhe3zc0w" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Async)
</div>
</button>
<button aria-controls="_1b0q88qgh" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_grxvbn41i" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Sync)
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_07zy0l588" class="leafygreen-ui-38lglc" id="_p6ob6ngk2" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"pipeline = BCON_NEW (\"pipeline\",\n \"[\",\n \"{\",\n \"$match\",\n \"{\",\n \"fullDocument.username\",\n BCON_UTF8 (\"alice\"),\n \"}\",\n \"}\",\n \"{\",\n \"$addFields\",\n \"{\",\n \"newField\",\n BCON_UTF8 (\"this is an added field!\"),\n \"}\",\n \"}\",\n \"]\");\n\nstream = mongoc_collection_watch (collection, pipeline, &opts);\nmongoc_change_stream_next (stream, &change);\nif (mongoc_change_stream_error_document (stream, &error, NULL)) {\n MONGOC_ERROR (\"%s\\n\", error.message);\n}\n\nmongoc_change_stream_destroy (stream);","programmingLanguage":"C"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">pipeline <!-- -->= <!-- --><span class="lg-highlight-variable lg-highlight-constant">BCON_NEW</span> (<span class="lg-highlight-string">"pipeline"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"["</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"{"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$match"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"{"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"fullDocument.username"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><span class="lg-highlight-variable lg-highlight-constant">BCON_UTF8</span> (<!-- --><span class="lg-highlight-string">"alice"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"{"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$addFields"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"{"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"newField"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><span class="lg-highlight-variable lg-highlight-constant">BCON_UTF8</span> (<!-- --><span class="lg-highlight-string">"this is an added field!"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"}"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"]"</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">stream <!-- -->= <!-- -->mongoc_collection_watch <!-- -->(collection, pipeline, &opts);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">mongoc_change_stream_next <!-- -->(<!-- -->stream, &change);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">if</span> <!-- -->(<!-- -->mongoc_change_stream_error_document (stream<!-- -->, <!-- -->&error, <span class="lg-highlight-variable lg-highlight-constant">NULL</span>)) {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><span class="lg-highlight-variable lg-highlight-constant">MONGOC_ERROR</span> (<span class="lg-highlight-string">"%s</span><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.<span class="lg-highlight-property">message</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">mongoc_change_stream_destroy <!-- -->(<!-- -->stream);</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-69-menu" aria-describedby="select-69-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-70" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r53:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r53:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r53:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r53:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_9zu8km8lx" class="leafygreen-ui-38lglc" id="_nmiqpqc7c" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var pipeline = new EmptyPipelineDefinition<changestreamdocument<bsondocument>>()\n .Match(change =>\n change.FullDocument[\"username\"] == \"alice\" ||\n change.OperationType == ChangeStreamOperationType.Delete)\n .AppendStage<changestreamdocument<bsondocument>, ChangeStreamDocument<bsondocument>, BsonDocument>(\n \"{ $addFields : { newField : 'this is an added field!' } }\");\n\nvar collection = database.GetCollection<bsondocument>(\"inventory\");\nusing (var cursor = collection.Watch(pipeline))\n{\n while (cursor.MoveNext() && cursor.Current.Count() == 0) { } // keep calling MoveNext until we've read the first batch\n var next = cursor.Current.First();\n}","programmingLanguage":"C#"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> pipeline = <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">EmptyPipelineDefinition</span><<span class="lg-highlight-title lg-highlight-class">ChangeStreamDocument</span><<span class="lg-highlight-title lg-highlight-class">BsonDocument</span>>>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .<span class="lg-highlight-title lg-highlight-class">Match</span>(<span class="lg-highlight-params lg-highlight-function">change</span><span class="lg-highlight-function"> =></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> change.<span class="lg-highlight-property">FullDocument</span>[<span class="lg-highlight-string">"username"</span>] == <span class="lg-highlight-string">"alice"</span> ||</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> change.<span class="lg-highlight-property">OperationType</span> == <span class="lg-highlight-title lg-highlight-class">ChangeStreamOperationType</span>.<span class="lg-highlight-property">Delete</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .<span class="lg-highlight-property">AppendStage</span><<span class="lg-highlight-title lg-highlight-class">ChangeStreamDocument</span><<span class="lg-highlight-title lg-highlight-class">BsonDocument</span>>, <span class="lg-highlight-title lg-highlight-class">ChangeStreamDocument</span><<span class="lg-highlight-title lg-highlight-class">BsonDocument</span>>, <span class="lg-highlight-title lg-highlight-class">BsonDocument</span>>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"{ $addFields : { newField : 'this is an added field!' } }"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> collection = database.<span class="lg-highlight-property">GetCollection</span><<span class="lg-highlight-title lg-highlight-class">BsonDocument</span>>(<span class="lg-highlight-string">"inventory"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">using (<span class="lg-highlight-keyword">var</span> cursor = collection.<span class="lg-highlight-title lg-highlight-class">Watch</span>(pipeline))</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">while</span> (cursor.<span class="lg-highlight-title lg-highlight-class">MoveNext</span>() && cursor.<span class="lg-highlight-property">Current</span>.<span class="lg-highlight-title lg-highlight-class">Count</span>() == <span class="lg-highlight-number">0</span>) { } <span class="lg-highlight-comment">// keep calling MoveNext</span><span class="lg-highlight-comment"> until we've read </span><span class="lg-highlight-comment">the first batch</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> next = cursor.<span class="lg-highlight-property">Current</span>.<span class="lg-highlight-title lg-highlight-class">First</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-72-menu" aria-describedby="select-72-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-73" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r54:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r54:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r54:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r54:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_1grddq8r5" class="leafygreen-ui-38lglc" id="_s7tofahjd" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"pipeline := mongo.Pipeline{bson.D{{\"$match\", bson.D{{\"$or\",\n\tbson.A{\n\t\tbson.D{{\"fullDocument.username\", \"alice\"}},\n\t\tbson.D{{\"operationType\", \"delete\"}}}}},\n}}}\ncs, err := coll.Watch(ctx, pipeline)\nassert.NoError(t, err)\ndefer cs.Close(ctx)\n\nok := cs.Next(ctx)\nnext := cs.Current\n","programmingLanguage":"Golang"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">pipeline <!-- -->:<!-- -->= mongo.<span class="lg-highlight-property">Pipeline</span>{bson.<span class="lg-highlight-property">D</span>{{<span class="lg-highlight-string">"$match"</span>, bson.<span class="lg-highlight-property">D</span>{{<span class="lg-highlight-string">"$or"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> bson.<span class="lg-highlight-property">A</span>{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> bson.<span class="lg-highlight-property">D</span>{{<span class="lg-highlight-string">"fullDocument.username"</span>, <span class="lg-highlight-string">"alice"</span>}},</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> bson.<span class="lg-highlight-property">D</span>{{<span class="lg-highlight-string">"operationType"</span>, <span class="lg-highlight-string">"delete"</span>}}}}},</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}}}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">cs<!-- -->, <!-- -->err := coll.<span class="lg-highlight-title lg-highlight-class">Watch</span>(ctx, pipeline)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">assert.<span class="lg-highlight-title lg-highlight-class">NoError</span>(t, err)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">defer cs.<span class="lg-highlight-title lg-highlight-class">Close</span>(ctx)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">ok <!-- -->:<!-- -->= cs.<span class="lg-highlight-title lg-highlight-class">Next</span>(ctx)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">next <!-- -->:<!-- -->= cs.<span class="lg-highlight-property">Current</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-75-menu" aria-describedby="select-75-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-76" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r55:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r55:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r55:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r55:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_29ezvqy0e" class="leafygreen-ui-38lglc" id="_uqe870d0s" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"MongoClient mongoClient = MongoClients.create(\"mongodb://<username>:<password>@<host>:<port>\");\n\n// Select the MongoDB database and collection to open the change stream against\n\nMongoDatabase db = mongoClient.getDatabase(\"myTargetDatabase\");\n\nMongoCollection<document> collection = db.getCollection(\"myTargetCollection\");\n\n// Create $match pipeline stage.\nList<bson> pipeline = singletonList(Aggregates.match(Filters.or(\n Document.parse(\"{'fullDocument.username': 'alice'}\"),\n Filters.in(\"operationType\", asList(\"delete\")))));\n\n// Create the change stream cursor, passing the pipeline to the\n// collection.watch() method\n\nMongoCursor<document> cursor = collection.watch(pipeline).iterator();","programmingLanguage":"Java"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-class">MongoClient</span> mongoClient = <span class="lg-highlight-title lg-highlight-class">MongoClients</span>.<span class="lg-highlight-title lg-highlight-function">create</span>(<span class="lg-highlight-string">"mongodb://<username>:<password>@<host>:<port>"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Select the MongoDB</span><span class="lg-highlight-comment"> database and collection </span><span class="lg-highlight-comment">to</span><span class="lg-highlight-comment"> open the change </span><span class="lg-highlight-comment">stream against</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-class">MongoDatabase</span> db = mongoClient.<span class="lg-highlight-title lg-highlight-function">getDatabase</span>(<span class="lg-highlight-string">"myTargetDatabase"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-class">MongoCollection</span><<span class="lg-highlight-title lg-highlight-class">Document</span>> collection = db.<span class="lg-highlight-title lg-highlight-function">getCollection</span>(<span class="lg-highlight-string">"myTargetCollection"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Create $match pipeline stage.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-class">List</span><<span class="lg-highlight-title lg-highlight-class">Bson</span>> pipeline = <span class="lg-highlight-title lg-highlight-function">singletonList</span>(<span class="lg-highlight-title lg-highlight-class">Aggregates</span>.<span class="lg-highlight-title lg-highlight-function">match</span>(<span class="lg-highlight-title lg-highlight-class">Filters</span>.<span class="lg-highlight-title lg-highlight-function">or</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-class">Document</span>.<span class="lg-highlight-title lg-highlight-function">parse</span>(<span class="lg-highlight-string">"{'fullDocument.username': 'alice'}"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-class">Filters</span>.<span class="lg-highlight-title lg-highlight-function">in</span>(<span class="lg-highlight-string">"operationType"</span>, <span class="lg-highlight-title lg-highlight-function">asList</span>(<span class="lg-highlight-string">"delete"</span>)))));</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Create the change </span><span class="lg-highlight-comment">stream cursor,</span><span class="lg-highlight-comment"> passing the pipeline </span><span class="lg-highlight-comment">to the</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// collection.watch() method</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-class">MongoCursor</span><<span class="lg-highlight-title lg-highlight-class">Document</span>> cursor = collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(pipeline).<span class="lg-highlight-title lg-highlight-function">iterator</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-78-menu" aria-describedby="select-78-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-79" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r56:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r56:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r56:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r56:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
list includes a single
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
stage that
filters for any operations that meet one or both of the following criteria:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
username
</code>
value is
<code class="leafygreen-ui-1l06pbn">
alice
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
operationType
</code>
value is
<code class="leafygreen-ui-1l06pbn">
delete
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Passing the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
watch()
</code>
</a>
method directs the
change stream to return notifications after passing them through the
specified
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_nm1wale0r" class="leafygreen-ui-38lglc" id="_b0pc58j9z" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"val pipeline = listOf(\n Aggregates.match(\n or(\n eq(\"fullDocument.username\", \"alice\"),\n `in`(\"operationType\", listOf(\"delete\"))\n )\n))\n\nval job = launch {\n val changeStream = collection.watch(pipeline)\n changeStream.collect {\n println(\"Received a change event: $it\")\n }\n}","programmingLanguage":"Kotlin"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">val pipeline = <span class="lg-highlight-title lg-highlight-function">listOf</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-class">Aggregates</span>.<span class="lg-highlight-title lg-highlight-function">match</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">or</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">eq</span>(<span class="lg-highlight-string">"fullDocument.username"</span>, <span class="lg-highlight-string">"alice"</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">`in`</span>(<span class="lg-highlight-string">"operationType"</span>, <span class="lg-highlight-title lg-highlight-function">listOf</span>(<span class="lg-highlight-string">"delete"</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">val job = launch {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> val changeStream = collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(pipeline)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<span class="lg-highlight-property">collect</span> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">println</span>(<span class="lg-highlight-string">"Received a change event: $it"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-81-menu" aria-describedby="select-81-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-82" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r57:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r57:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r57:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r57:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
list includes a single
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
stage that
filters for any operations that meet one or both of the following criteria:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
username
</code>
value is
<code class="leafygreen-ui-1l06pbn">
alice
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
operationType
</code>
value is
<code class="leafygreen-ui-1l06pbn">
delete
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Passing the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
watch()
</code>
</a>
method directs the
change stream to return notifications after passing them through the
specified
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_zd5s9waw0" class="leafygreen-ui-38lglc" id="_kjc6x1bhs" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"pipeline = [\n {\"$match\": {\"fullDocument.username\": \"alice\"}},\n {\"$addFields\": {\"newField\": \"this is an added field!\"}},\n]\ncursor = db.inventory.watch(pipeline=pipeline)\ndocument = await cursor.next()","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">pipeline = [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"$match"</span>: {<span class="lg-highlight-string">"fullDocument.username"</span>: <span class="lg-highlight-string">"alice"</span>}},</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"$addFields"</span>: {<span class="lg-highlight-string">"newField"</span>: <span class="lg-highlight-string">"this is an added field!"</span>}},</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">cursor = db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">watch</span>(pipeline=pipeline)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-variable lg-highlight-language">document</span> = <span class="lg-highlight-keyword">await</span> cursor.<span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-84-menu" aria-describedby="select-84-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-85" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r58:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r58:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r58:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r58:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_7azbakoen" class="" id="_b19zfbsnx" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The following example uses stream to process the change events.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const pipeline = [\n { $match: { 'fullDocument.username': 'alice' } },\n { $addFields: { newField: 'this is an added field!' } }\n];\n\nconst collection = db.collection('inventory');\nconst changeStream = collection.watch(pipeline);\nchangeStream.on('change', next => {\n // process next document\n});","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> pipeline <!-- -->= <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-string">'fullDocument.username'</span>: <span class="lg-highlight-string">'alice'</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$addFields</span>: <!-- -->{ <span class="lg-highlight-attr">newField</span>: <span class="lg-highlight-string">'this is an added field!'</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> collection <!-- -->= <!-- -->db.<span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">'inventory'</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> changeStream <!-- -->= <!-- -->collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->pipeline)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">changeStream.<span class="lg-highlight-title lg-highlight-function">on</span>(<span class="lg-highlight-string">'change'</span>, <span class="lg-highlight-params lg-highlight-function">next</span><span class="lg-highlight-function"> =></span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// process next document</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">})<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-87-menu" aria-describedby="select-87-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-88" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r59:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r59:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r59:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r59:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Alternatively, you can also use iterator to process the change events:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const changeStreamIterator = collection.watch(pipeline);\nconst next = await changeStreamIterator.next();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> changeStreamIterator <!-- -->= <!-- -->collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->pipeline)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> next <!-- -->= <span class="lg-highlight-keyword">await</span> changeStreamIterator.<span class="lg-highlight-title lg-highlight-function">next</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-90-menu" aria-describedby="select-90-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-91" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5a:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5a:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5a:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5a:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_yv5g519x5" class="leafygreen-ui-38lglc" id="_k3jv3mjf4" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"$pipeline = [\n ['$match' => ['fullDocument.username' => 'alice']],\n ['$addFields' => ['newField' => 'this is an added field!']],\n];\n$changeStream = $db->inventory->watch($pipeline);\n$changeStream->rewind();\n\n$firstChange = $changeStream->current();\n\n$changeStream->next();\n\n$secondChange = $changeStream->current();","programmingLanguage":"PHP"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">$pipeline = [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'$match'</span> => [<span class="lg-highlight-string">'fullDocument.username'</span> => <span class="lg-highlight-string">'alice'</span>]],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">'$addFields'</span> => [<span class="lg-highlight-string">'newField'</span> => <span class="lg-highlight-string">'this is an added field!'</span>]],</td></tr><tr class=""><td class="leafygreen-ui-7razhx">];</td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream = $db->inventory-<!-- -->><span class="lg-highlight-title lg-highlight-function">watch</span>($pipeline);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream-<!-- -->><span class="lg-highlight-title lg-highlight-function">rewind</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$firstChange = $changeStream-><!-- --><span class="lg-highlight-title lg-highlight-function">current</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream-<!-- -->><span class="lg-highlight-title lg-highlight-function">next</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$secondChange = $changeStream-><!-- --><span class="lg-highlight-title lg-highlight-function">current</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-93-menu" aria-describedby="select-93-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-94" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5b:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5b:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5b:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5b:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_jp1bviyet" class="leafygreen-ui-38lglc" id="_t8isihbim" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"pipeline = [\n {\"$match\": {\"fullDocument.username\": \"alice\"}},\n {\"$addFields\": {\"newField\": \"this is an added field!\"}},\n]\ncursor = db.inventory.watch(pipeline=pipeline)\nnext(cursor)","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">pipeline = [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"$match"</span>: {<span class="lg-highlight-string">"fullDocument.username"</span>: <span class="lg-highlight-string">"alice"</span>}},</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {<span class="lg-highlight-string">"$addFields"</span>: {<span class="lg-highlight-string">"newField"</span>: <span class="lg-highlight-string">"this is an added field!"</span>}},</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">cursor = db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">watch</span>(pipeline=pipeline)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-function">next</span>(cursor)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-96-menu" aria-describedby="select-96-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-97" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5c:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5c:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5c:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5c:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_9wfnnq7x7" class="leafygreen-ui-38lglc" id="_kneqmm3un" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</div>
<div>
<div aria-labelledby="_2m3riw5g1" class="leafygreen-ui-38lglc" id="_rhnmbyea2" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let pipeline: [BSONDocument] = [\n [\"$match\": [\"fullDocument.username\": \"alice\"]],\n [\"$addFields\": [\"newField\": \"this is an added field!\"]]\n]\nlet inventory = db.collection(\"inventory\")\n\n// Option 1: use next() to iterate\nlet next = inventory.watch(pipeline, withEventType: BSONDocument.self).flatMap { changeStream in\n changeStream.next()\n}\n\n// Option 2: register a callback to execute for each document\nlet result = inventory.watch(pipeline, withEventType: BSONDocument.self).flatMap { changeStream in\n changeStream.forEach { event in\n // process event\n print(event)\n }\n}","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> <span class="lg-highlight-attr">pipeline</span>: [<span class="lg-highlight-title lg-highlight-class">BSONDocument</span>] = [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">"$match"</span>: [<span class="lg-highlight-string">"fullDocument.username"</span>: <span class="lg-highlight-string">"alice"</span>]],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">"$addFields"</span>: [<span class="lg-highlight-string">"newField"</span>: <span class="lg-highlight-string">"this is an added field!"</span>]]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> inventory = db.<!-- --><span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">"inventory"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Option 1: use next() to iterate</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> next = inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->pipeline<!-- -->, <span class="lg-highlight-attr">withEventType</span>: <span class="lg-highlight-title lg-highlight-class">BSONDocument</span>.<span class="lg-highlight-property">self</span>).<span class="lg-highlight-property">flatMap</span> { changeStream <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<!-- --><span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Option 2:</span><span class="lg-highlight-comment"> register a callback </span><span class="lg-highlight-comment">to</span><span class="lg-highlight-comment"> execute for each </span><span class="lg-highlight-comment">document</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> result = inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->pipeline<!-- -->, <span class="lg-highlight-attr">withEventType</span>: <span class="lg-highlight-title lg-highlight-class">BSONDocument</span>.<span class="lg-highlight-property">self</span>).<span class="lg-highlight-property">flatMap</span> { changeStream <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<!-- --><!-- --><span class="lg-highlight-property">forEach</span> { event <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// process event</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(event)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-99-menu" aria-describedby="select-99-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-100" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5d:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5d:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5d:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5d:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_j4w8jtp5b" class="leafygreen-ui-38lglc" id="_t248nwyz0" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can control
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
by
providing an array of one or more of the following pipeline stages when
configuring the change stream:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith">
<code class="leafygreen-ui-1l06pbn">
$replaceWith
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/redact/#mongodb-pipeline-pipe.-redact">
<code class="leafygreen-ui-1l06pbn">
$redact
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let pipeline: [BSONDocument] = [\n [\"$match\": [\"fullDocument.username\": \"alice\"]],\n [\"$addFields\": [\"newField\": \"this is an added field!\"]]\n]\nlet inventory = db.collection(\"inventory\")\nlet changeStream = try inventory.watch(pipeline, withEventType: BSONDocument.self)\nlet next = changeStream.next()","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> <span class="lg-highlight-attr">pipeline</span>: [<span class="lg-highlight-title lg-highlight-class">BSONDocument</span>] = [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">"$match"</span>: [<span class="lg-highlight-string">"fullDocument.username"</span>: <span class="lg-highlight-string">"alice"</span>]],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> [<span class="lg-highlight-string">"$addFields"</span>: [<span class="lg-highlight-string">"newField"</span>: <span class="lg-highlight-string">"this is an added field!"</span>]]</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> inventory = db.<!-- --><span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">"inventory"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> changeStream <!-- -->= <span class="lg-highlight-keyword">try</span> inventory.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->pipeline<!-- -->, <span class="lg-highlight-attr">withEventType</span>: <span class="lg-highlight-title lg-highlight-class">BSONDocument</span>.<span class="lg-highlight-property">self</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> next = changeStream.<!-- --><span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-102-menu" aria-describedby="select-102-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-103" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5e:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5e:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5e:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5e:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-event-id">
_id
</a>
field of the change stream
event document act as the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-resume" target="_self">
<span>
resume token
</span>
</a>
. Do not use the pipeline to modify or remove
the change stream event's
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 4.2, change streams will throw an exception if
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-modify-output" target="_self">
<span>
change stream aggregation pipeline
</span>
</a>
modifies an event's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-event-id">
_id
</a>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
Change Events
</a>
for more information on the change stream
response document format.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-streams-updateLookup">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Lookup Full Document for Update Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#lookup-full-document-for-update-operations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="lookup-full-document-for-update-operations">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
By default, change streams only return the delta of fields during
the update operation. However, you can configure the change stream
to return the most current majority-committed version of the updated
document.
</p>
<hr class="css-1unva0n e4ru7ww0"/>
<p class="leafygreen-ui-1kp3ins">
➤
<!-- -->
Use the
<strong>
Select your language
</strong>
drop-down menu in the
upper-right to set the language of the examples on this page.
</p>
<hr class="css-1unva0n e4ru7ww0"/>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-f2qztv" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-15">
<div aria-label="Tabs to describe usage of drivers" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_xskdpr968" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_qd64ddfw5" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C
</div>
</button>
<button aria-controls="_jdc41dxh7" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_iwamzx16l" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C#
</div>
</button>
<button aria-controls="_xhw67h1ha" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_6igv570o4" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Go
</div>
</button>
<button aria-controls="_1n9di9blv" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_x5lhkfjsn" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Java (Sync)
</div>
</button>
<button aria-controls="_qdyg93f72" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_exhplrjnj" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Kotlin (Coroutine)
</div>
</button>
<button aria-controls="_s06ozjcp8" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_vv5daqgg0" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Motor
</div>
</button>
<button aria-controls="_11epz84bg" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_uaht2y55i" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Node.js
</div>
</button>
<button aria-controls="_i42bsi8dk" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_r6bl8cy7n" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
PHP
</div>
</button>
<button aria-controls="_vzycq0cmu" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_gkx80m1jo" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Python
</div>
</button>
<button aria-controls="_oet4qpkwt" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_r6fpp7joj" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Ruby
</div>
</button>
<button aria-controls="_h0q7onz89" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_vw8mok3ju" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Async)
</div>
</button>
<button aria-controls="_ezwxk2d77" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_rjf7ys1kt" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Sync)
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_h02h33ruv" class="leafygreen-ui-38lglc" id="_s96309rno" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the updated
document, pass the
<code class="leafygreen-ui-1l06pbn">
"fullDocument"
</code>
option with the
<code class="leafygreen-ui-1l06pbn">
"updateLookup"
</code>
value to the
<code class="leafygreen-ui-1l06pbn">
mongoc_collection_watch
</code>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"BSON_APPEND_UTF8 (&opts, \"fullDocument\", \"updateLookup\");\nstream = mongoc_collection_watch (collection, pipeline, &opts);\nmongoc_change_stream_next (stream, &change);\nif (mongoc_change_stream_error_document (stream, &error, NULL)) {\n MONGOC_ERROR (\"%s\\n\", error.message);\n}\n\nmongoc_change_stream_destroy (stream);","programmingLanguage":"C"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><!-- --><span class="lg-highlight-variable lg-highlight-constant">BSON_APPEND_UTF8</span> (&opts, <!-- --><span class="lg-highlight-string">"fullDocument"</span>, <span class="lg-highlight-string">"updateLookup"</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">stream <!-- -->= <!-- -->mongoc_collection_watch <!-- -->(collection, pipeline, &opts);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">mongoc_change_stream_next <!-- -->(<!-- -->stream, &change);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">if</span> <!-- -->(<!-- -->mongoc_change_stream_error_document (stream<!-- -->, <!-- -->&error, <span class="lg-highlight-variable lg-highlight-constant">NULL</span>)) {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><span class="lg-highlight-variable lg-highlight-constant">MONGOC_ERROR</span> (<span class="lg-highlight-string">"%s</span><!-- --><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">"</span>, error.<span class="lg-highlight-property">message</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">mongoc_change_stream_destroy <!-- -->(<!-- -->stream);</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-105-menu" aria-describedby="select-105-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-106" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5f:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5f:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5f:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5f:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_u78pzevlf" class="leafygreen-ui-38lglc" id="_zadvjpezf" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the updated
document, pass
<code class="leafygreen-ui-1l06pbn">
"FullDocument = ChangeStreamFullDocumentOption.UpdateLookup"
</code>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
db.collection.watch()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
FullDocument
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };\nvar cursor = inventory.Watch(options);\nwhile (cursor.MoveNext() && cursor.Current.Count() == 0) { } // keep calling MoveNext until we've read the first batch\nvar next = cursor.Current.First();\ncursor.Dispose();","programmingLanguage":"C#"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> options = <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span> { <span class="lg-highlight-title lg-highlight-class">FullDocument</span> = <span class="lg-highlight-title lg-highlight-class">ChangeStreamFullDocumentOption</span>.<span class="lg-highlight-property">UpdateLookup</span> };</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> cursor = inventory.<span class="lg-highlight-title lg-highlight-class">Watch</span>(options);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">while</span> (cursor.<span class="lg-highlight-title lg-highlight-class">MoveNext</span>() && cursor.<span class="lg-highlight-property">Current</span>.<span class="lg-highlight-title lg-highlight-class">Count</span>() == <span class="lg-highlight-number">0</span>) { } <span class="lg-highlight-comment">// keep calling MoveNext</span><span class="lg-highlight-comment"> until we've read </span><span class="lg-highlight-comment">the first batch</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> next = cursor.<span class="lg-highlight-property">Current</span>.<span class="lg-highlight-title lg-highlight-class">First</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">cursor.<span class="lg-highlight-title lg-highlight-class">Dispose</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-108-menu" aria-describedby="select-108-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-109" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5g:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5g:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5g:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5g:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_vayst26z3" class="leafygreen-ui-38lglc" id="_2h37uoidt" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the
updated document,
<code class="leafygreen-ui-1l06pbn">
SetFullDocument(options.UpdateLookup)
</code>
change stream option.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\ncs, err := coll.Watch(ctx, mongo.Pipeline{}, options.ChangeStream().SetFullDocument(options.UpdateLookup))\nassert.NoError(t, err)\ndefer cs.Close(ctx)\n\nok := cs.Next(ctx)\nnext := cs.Current\n","programmingLanguage":"Golang"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cs<!-- -->, <!-- -->err := coll.<span class="lg-highlight-title lg-highlight-class">Watch</span>(ctx, mongo.<span class="lg-highlight-property">Pipeline</span>{}, options.<span class="lg-highlight-title lg-highlight-class">ChangeStream</span>().<span class="lg-highlight-title lg-highlight-class">SetFullDocument</span>(options.<span class="lg-highlight-property">UpdateLookup</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx">assert.<span class="lg-highlight-title lg-highlight-class">NoError</span>(t, err)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">defer cs.<span class="lg-highlight-title lg-highlight-class">Close</span>(ctx)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">ok <!-- -->:<!-- -->= cs.<span class="lg-highlight-title lg-highlight-class">Next</span>(ctx)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">next <!-- -->:<!-- -->= cs.<span class="lg-highlight-property">Current</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-111-menu" aria-describedby="select-111-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-112" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5h:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5h:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5h:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5h:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_qe1x9dum4" class="leafygreen-ui-38lglc" id="_qmhfhoxxt" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the updated
document, pass
<code class="leafygreen-ui-1l06pbn">
FullDocument.UPDATE_LOOKUP
</code>
to the
<code class="leafygreen-ui-1l06pbn">
db.collection.watch.fullDocument()
</code>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
FullDocument
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cursor = inventory.watch().fullDocument(FullDocument.UPDATE_LOOKUP).iterator();\nnext = cursor.next();","programmingLanguage":"Java"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cursor = inventory.<span class="lg-highlight-title lg-highlight-function">watch</span>().<span class="lg-highlight-title lg-highlight-function">fullDocument</span>(<span class="lg-highlight-title lg-highlight-class">FullDocument</span>.<span class="lg-highlight-property">UPDATE_LOOKUP</span>).<span class="lg-highlight-title lg-highlight-function">iterator</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">next = cursor.<span class="lg-highlight-title lg-highlight-function">next</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-114-menu" aria-describedby="select-114-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-115" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5i:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5i:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5i:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5i:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_wmd5wlyt2" class="leafygreen-ui-38lglc" id="_nfcha1pdw" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the updated
document, pass
<code class="leafygreen-ui-1l06pbn">
FullDocument.UPDATE_LOOKUP
</code>
to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mongodb.github.io/mongo-java-driver/4.11/apidocs/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-change-stream-flow/full-document.html" rel="noopener noreferrer">
<span>
ChangeStreamFlow.fullDocument()
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
FullDocument
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"val job = launch {\n val changeStream = collection.watch()\n .fullDocument(FullDocument.UPDATE_LOOKUP)\n changeStream.collect {\n println(it)\n }\n}","programmingLanguage":"Kotlin"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">val job = launch {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> val changeStream = collection.<span class="lg-highlight-title lg-highlight-function">watch</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .<span class="lg-highlight-title lg-highlight-function">fullDocument</span>(<span class="lg-highlight-title lg-highlight-class">FullDocument</span>.<span class="lg-highlight-property">UPDATE_LOOKUP</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<span class="lg-highlight-property">collect</span> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">println</span>(it)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-117-menu" aria-describedby="select-117-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-118" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5j:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5j:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5j:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5j:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_0u1adh1tb" class="leafygreen-ui-38lglc" id="_9aipg3c46" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the updated
document, pass
<code class="leafygreen-ui-1l06pbn">
full_document='updateLookup'
</code>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
db.collection.watch()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
`full_document
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cursor = db.inventory.watch(full_document=\"updateLookup\")\ndocument = await cursor.next()","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cursor = db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">watch</span>(full_document=<span class="lg-highlight-string">"updateLookup"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-variable lg-highlight-language">document</span> = <span class="lg-highlight-keyword">await</span> cursor.<span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-120-menu" aria-describedby="select-120-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-121" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5k:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5k:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5k:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5k:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_ry727povr" class="" id="_tpd25tafw" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the updated
document, pass
<code class="leafygreen-ui-1l06pbn">
{ fullDocument: 'updateLookup' }
</code>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
db.collection.watch()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example uses stream to process the change events.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const collection = db.collection('inventory');\nconst changeStream = collection.watch([], { fullDocument: 'updateLookup' });\nchangeStream.on('change', next => {\n // process next document\n});","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> collection <!-- -->= <!-- -->db.<span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">'inventory'</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> changeStream <!-- -->= <!-- -->collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->[<!-- -->]<!-- -->, <!-- -->{ <span class="lg-highlight-attr">fullDocument</span>: <span class="lg-highlight-string">'updateLookup'</span> })<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">changeStream.<span class="lg-highlight-title lg-highlight-function">on</span>(<span class="lg-highlight-string">'change'</span>, <span class="lg-highlight-params lg-highlight-function">next</span><span class="lg-highlight-function"> =></span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// process next document</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">})<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-123-menu" aria-describedby="select-123-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-124" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5l:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5l:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5l:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5l:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Alternatively, you can also use iterator to process the change events:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const changeStreamIterator = collection.watch([], { fullDocument: 'updateLookup' });\nconst next = await changeStreamIterator.next();","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> changeStreamIterator <!-- -->= <!-- -->collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->[<!-- -->]<!-- -->, <!-- -->{ <span class="lg-highlight-attr">fullDocument</span>: <span class="lg-highlight-string">'updateLookup'</span> })<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> next <!-- -->= <span class="lg-highlight-keyword">await</span> changeStreamIterator.<span class="lg-highlight-title lg-highlight-function">next</span>(<!-- -->)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-126-menu" aria-describedby="select-126-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-127" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5m:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5m:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5m:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5m:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_a0s2v638i" class="leafygreen-ui-38lglc" id="_06lrd79pa" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current
majority-committed version of the updated document, pass
<code class="leafygreen-ui-1l06pbn">
"fullDocument' => \MongoDB\Operation\ChangeStreamCommand::FULL_DOCUMENT_UPDATE_LOOKUP"
</code>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.watch/#mongodb-method-db.watch">
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"$changeStream = $db->inventory->watch([], ['fullDocument' => \\MongoDB\\Operation\\Watch::FULL_DOCUMENT_UPDATE_LOOKUP]);\n$changeStream->rewind();\n\n$firstChange = $changeStream->current();\n\n$changeStream->next();\n\n$secondChange = $changeStream->current();","programmingLanguage":"PHP"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">$changeStream = $db->inventory-><span class="lg-highlight-title lg-highlight-function">watch</span>([<!-- -->], [<span class="lg-highlight-string">'fullDocument'</span> => \<span class="lg-highlight-title lg-highlight-class">MongoDB</span>\<span class="lg-highlight-title lg-highlight-class">Operation</span>\<span class="lg-highlight-title lg-highlight-class">Watch</span>::<span class="lg-highlight-variable lg-highlight-constant">FULL_DOCUMENT_UPDATE_LOOKUP</span>]);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream-<!-- -->><span class="lg-highlight-title lg-highlight-function">rewind</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$firstChange = $changeStream-><!-- --><span class="lg-highlight-title lg-highlight-function">current</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream-<!-- -->><span class="lg-highlight-title lg-highlight-function">next</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$secondChange = $changeStream-><!-- --><span class="lg-highlight-title lg-highlight-function">current</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-129-menu" aria-describedby="select-129-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-130" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5n:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5n:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5n:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5n:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_2tigatnpm" class="leafygreen-ui-38lglc" id="_odzgh3cr7" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the updated
document, pass
<code class="leafygreen-ui-1l06pbn">
full_document='updateLookup'
</code>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
db.collection.watch()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
full_document
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cursor = db.inventory.watch(full_document=\"updateLookup\")\nnext(cursor)","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cursor = db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">watch</span>(full_document=<span class="lg-highlight-string">"updateLookup"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-function">next</span>(cursor)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-132-menu" aria-describedby="select-132-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-133" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5o:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5o:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5o:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5o:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_nd5lxl93q" class="leafygreen-ui-38lglc" id="_nv2lc19k8" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of the updated
document, pass
<code class="leafygreen-ui-1l06pbn">
full_document: 'updateLookup'
</code>
to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.watch/#mongodb-method-db.watch">
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
In the example below, all update operations notifications
include a
<code class="leafygreen-ui-1l06pbn">
full_document
</code>
field that represents the
<em>
current
</em>
version of the document affected by the update operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\ncursor = inventory.watch([], full_document: 'updateLookup').to_enum\nnext_change = cursor.next\n","programmingLanguage":"Ruby"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cursor <!-- -->= <!-- -->inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->[<!-- -->], <span class="lg-highlight-attr">full_document</span>: <span class="lg-highlight-string">'updateLookup'</span>).<span class="lg-highlight-property">to_enum</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">next_change <!-- -->= <!-- -->cursor.<span class="lg-highlight-property">next</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-135-menu" aria-describedby="select-135-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-136" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5p:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5p:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5p:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5p:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_7b9yxzq3i" class="leafygreen-ui-38lglc" id="_aom82g0p7" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of
the updated document, pass
<code class="leafygreen-ui-1l06pbn">
options:
ChangeStreamOptions(fullDocument: .updateLookup)
</code>
to the
<code class="leafygreen-ui-1l06pbn">
watch()
</code>
method.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let inventory = db.collection(\"inventory\")\n\n// Option 1: use next() to iterate\nlet next = inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))\n .flatMap { changeStream in\n changeStream.next()\n }\n\n// Option 2: register a callback to execute for each document\nlet result = inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))\n .flatMap { changeStream in\n changeStream.forEach { event in\n // process event\n print(event)\n }\n }","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> inventory = db.<!-- --><span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">"inventory"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Option 1: use next() to iterate</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> next = inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- --><!-- --><span class="lg-highlight-attr">options</span>: <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span>(<!-- --><!-- --><span class="lg-highlight-attr">fullDocument</span>: .<span class="lg-highlight-property">updateLookup</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .<span class="lg-highlight-property">flatMap</span> { changeStream <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<!-- --><span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// Option 2:</span><span class="lg-highlight-comment"> register a callback </span><span class="lg-highlight-comment">to</span><span class="lg-highlight-comment"> execute for each </span><span class="lg-highlight-comment">document</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> result = inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- --><!-- --><span class="lg-highlight-attr">options</span>: <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span>(<!-- --><!-- --><span class="lg-highlight-attr">fullDocument</span>: .<span class="lg-highlight-property">updateLookup</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .<span class="lg-highlight-property">flatMap</span> { changeStream <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<!-- --><!-- --><span class="lg-highlight-property">forEach</span> { event <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// process event</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(event)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-138-menu" aria-describedby="select-138-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-139" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5q:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5q:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5q:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5q:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_wb7gsbvkw" class="leafygreen-ui-38lglc" id="_1qc8icz5k" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
To return the most current majority-committed version of
the updated document, pass
<code class="leafygreen-ui-1l06pbn">
options:
ChangeStreamOptions(fullDocument: .updateLookup)
</code>
to the
<code class="leafygreen-ui-1l06pbn">
watch()
</code>
method.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let inventory = db.collection(\"inventory\")\nlet changeStream = try inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))\nlet next = changeStream.next()","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> inventory = db.<!-- --><span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">"inventory"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> changeStream <!-- -->= <span class="lg-highlight-keyword">try</span> inventory.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- --><!-- --><span class="lg-highlight-attr">options</span>: <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span>(<!-- --><!-- --><span class="lg-highlight-attr">fullDocument</span>: <!-- -->.<span class="lg-highlight-property">updateLookup</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> next = changeStream.<!-- --><span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-141-menu" aria-describedby="select-141-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-142" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5r:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5r:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5r:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5r:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If there are one or more majority-committed operations that modified
the updated document
<em>
after
</em>
the update operation but
<em>
before
</em>
the
lookup, the full document returned may differ significantly from the
document at the time of the update operation.
</p>
<p class="leafygreen-ui-1kp3ins">
However, the deltas included in the change stream document always
correctly describe the watched collection changes that applied to
that change stream event.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
field for an update event may be missing if one
of the following is true:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the document is deleted or if the collection is dropped in
between the update and the lookup.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the update changes the values for at least one of the fields in
that collection's shard key.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
Change Events
</a>
for more information on the change
stream response document format.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-stream-resume">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Resume a Change Stream
<a class="headerlink leafygreen-ui-1kepx47" href="#resume-a-change-stream" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resume-a-change-stream">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Change streams are resumable by specifying a resume token to either
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-resume-after" target="_self">
<span>
resumeAfter
</span>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-start-after" target="_self">
<span>
startAfter
</span>
</a>
when opening the cursor.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-stream-resume-after">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
for Change Streams
<a class="headerlink leafygreen-ui-1kepx47" href="#resumeafter-for-change-streams" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resumeafter-for-change-streams">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can resume a change stream after a specific event by passing a resume token
to
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
when opening the cursor.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-resume-token" target="_self">
<span>
Resume Tokens
</span>
</a>
for more information on the resume token.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The oplog must have enough history to locate the operation
associated with the token or the timestamp, if the timestamp is in
the past.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You cannot use
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
to resume a change stream after an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/invalidate/#std-label-change-event-invalidate">
invalidate event
</a>
(for example, a collection
drop or rename) closes the stream. Instead, you can use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-start-after" target="_self">
<span>
startAfter
</span>
</a>
to start a new change
stream after an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/invalidate/#std-label-change-event-invalidate">
invalidate event
<!-- -->
.
</a>
</p>
</li>
</ul>
</div>
</div>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-f2qztv" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-18">
<div aria-label="Tabs to describe usage of drivers" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_huoao38b9" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_rtj6gy74d" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C
</div>
</button>
<button aria-controls="_o4dj64qb7" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_uotlrk8de" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
C#
</div>
</button>
<button aria-controls="_b3vwuhktz" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_d7r22kbe5" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Go
</div>
</button>
<button aria-controls="_sjsoe8ylt" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_w9ikp4jxj" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Java (Sync)
</div>
</button>
<button aria-controls="_t5g99ry1w" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_zjcgpk2x4" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Kotlin (Coroutine)
</div>
</button>
<button aria-controls="_y52srqjf7" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_iyfknlen3" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Motor
</div>
</button>
<button aria-controls="_sr48g71wg" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_5foqbamql" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Node.js
</div>
</button>
<button aria-controls="_07v0c9f2n" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_epqhvxmd8" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
PHP
</div>
</button>
<button aria-controls="_dgg1n4h89" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_hg2337mor" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Python
</div>
</button>
<button aria-controls="_rukn0be68" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_93mxs5tv1" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Ruby
</div>
</button>
<button aria-controls="_3g1em1mmk" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_2if2ei05d" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Async)
</div>
</button>
<button aria-controls="_d4dd6j67v" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_lv0p5zty6" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Swift (Sync)
</div>
</button>
</div>
<div class="leafygreen-ui-ov1ktg">
<div class="leafygreen-ui-1sg2lsz">
</div>
</div>
</div>
<div class="lg-ui-tab-panels-0000" data-lgid="lg-tabs-tab_panels">
<div>
<div aria-labelledby="_c5ypu7d4n" class="leafygreen-ui-38lglc" id="_fvtbmsj5v" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
In the example below, the
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option is appended to the stream options
to recreate the stream after it has been destroyed. Passing the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
to
the change stream attempts to resume notifications starting after the
operation specified.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"stream = mongoc_collection_watch (collection, pipeline, NULL);\nif (mongoc_change_stream_next (stream, &change)) {\n resume_token = mongoc_change_stream_get_resume_token (stream);\n BSON_APPEND_DOCUMENT (&opts, \"resumeAfter\", resume_token);\n\n mongoc_change_stream_destroy (stream);\n stream = mongoc_collection_watch (collection, pipeline, &opts);\n mongoc_change_stream_next (stream, &change);\n mongoc_change_stream_destroy (stream);\n} else {\n if (mongoc_change_stream_error_document (stream, &error, NULL)) {\n MONGOC_ERROR (\"%s\\n\", error.message);\n }\n\n mongoc_change_stream_destroy (stream);\n}","programmingLanguage":"C"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light none leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">stream = mongoc_collection_watch (collection, pipeline, NULL);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">if (mongoc_change_stream_next (stream, &change)) {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> resume_token = mongoc_change_stream_get_resume_token (stream);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> BSON_APPEND_DOCUMENT (&opts, "resumeAfter", resume_token);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_change_stream_destroy (stream);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> stream = mongoc_collection_watch (collection, pipeline, &opts);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_change_stream_next (stream, &change);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_change_stream_destroy (stream);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} else {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> if (mongoc_change_stream_error_document (stream, &error, NULL)) {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> MONGOC_ERROR ("%s\n", error.message);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> mongoc_change_stream_destroy (stream);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_p8seijad4" class="leafygreen-ui-38lglc" id="_v1iiy207d" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
In the example below, the
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
is retrieved from the last change stream document
and passed to the
<code class="leafygreen-ui-1l06pbn">
Watch()
</code>
method as an option. Passing the
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
to the
<code class="leafygreen-ui-1l06pbn">
Watch()
</code>
method directs
the change stream to attempt to resume notifications starting after the
operation specified in the resume token.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" var resumeToken = previousCursor.GetResumeToken();\n var options = new ChangeStreamOptions { ResumeAfter = resumeToken };\n var cursor = inventory.Watch(options);\n cursor.MoveNext();\n var next = cursor.Current.First();\n cursor.Dispose();","programmingLanguage":"C#"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> resumeToken = previousCursor.<span class="lg-highlight-title lg-highlight-class">GetResumeToken</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> options = <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span> { <span class="lg-highlight-title lg-highlight-class">ResumeAfter</span> = resumeToken };</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> cursor = inventory.<span class="lg-highlight-title lg-highlight-class">Watch</span>(options);</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor.<span class="lg-highlight-title lg-highlight-class">MoveNext</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> next = cursor.<span class="lg-highlight-property">Current</span>.<span class="lg-highlight-title lg-highlight-class">First</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor.<span class="lg-highlight-title lg-highlight-class">Dispose</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-145-menu" aria-describedby="select-145-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-146" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5s:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5s:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5s:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5s:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_uwoxwj9ro" class="leafygreen-ui-38lglc" id="_nt2it2lq3" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://godoc.org/go.mongodb.org/mongo-driver/mongo/options#ChangeStreamOptions.SetResumeAfter" rel="noopener noreferrer">
<span>
ChangeStreamOptions.SetResumeAfter
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
to specify the resume
token for the change stream. If the resumeAfter option is set,
the change stream resumes notifications after the operation
specified in the resume token. The
<code class="leafygreen-ui-1l06pbn">
SetResumeAfter
</code>
takes a
value that must resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
in the example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"resumeToken := original.ResumeToken()\n\ncs, err := coll.Watch(ctx, mongo.Pipeline{}, options.ChangeStream().SetResumeAfter(resumeToken))\nassert.NoError(t, err)\ndefer cs.Close(ctx)\n\nok = cs.Next(ctx)\nresult := cs.Current\n","programmingLanguage":"Golang"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">resumeToken <!-- -->:<!-- -->= original.<span class="lg-highlight-title lg-highlight-class">ResumeToken</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">cs<!-- -->, <!-- -->err := coll.<span class="lg-highlight-title lg-highlight-class">Watch</span>(ctx, mongo.<span class="lg-highlight-property">Pipeline</span>{}, options.<span class="lg-highlight-title lg-highlight-class">ChangeStream</span>().<span class="lg-highlight-title lg-highlight-class">SetResumeAfter</span>(resumeToken))</td></tr><tr class=""><td class="leafygreen-ui-7razhx">assert.<span class="lg-highlight-title lg-highlight-class">NoError</span>(t, err)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">defer cs.<span class="lg-highlight-title lg-highlight-class">Close</span>(ctx)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">ok = cs.<span class="lg-highlight-title lg-highlight-class">Next</span>(ctx)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">result <!-- -->:<!-- -->= cs.<span class="lg-highlight-property">Current</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-148-menu" aria-describedby="select-148-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-149" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5t:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5t:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5t:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5t:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_pex2j9jh3" class="leafygreen-ui-38lglc" id="_5ieao0wdw" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
resumeAfter()
</code>
method to resume
notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resumeAfter()
</code>
method takes a value that must
resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"BsonDocument resumeToken = next.getResumeToken();\ncursor = inventory.watch().resumeAfter(resumeToken).iterator();\nnext = cursor.next();","programmingLanguage":"Java"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-class">BsonDocument</span> resumeToken = next.<span class="lg-highlight-title lg-highlight-function">getResumeToken</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">cursor = inventory.<span class="lg-highlight-title lg-highlight-function">watch</span>().<span class="lg-highlight-title lg-highlight-function">resumeAfter</span>(resumeToken).<span class="lg-highlight-title lg-highlight-function">iterator</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx">next = cursor.<span class="lg-highlight-title lg-highlight-function">next</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-151-menu" aria-describedby="select-151-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-152" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5u:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5u:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5u:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5u:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_mvma9nfmi" class="leafygreen-ui-38lglc" id="_wjk5s8wmb" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mongodb.github.io/mongo-java-driver/4.11/apidocs/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-change-stream-flow/resume-after.html" rel="noopener noreferrer">
<span>
ChangeStreamFlow.resumeAfter()
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
method to resume notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resumeAfter()
</code>
method takes a value that must
resolve to a resume token, such as the
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
variable in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"val resumeToken = BsonDocument()\nval job = launch {\n val changeStream = collection.watch()\n .resumeAfter(resumeToken)\n changeStream.collect {\n println(it)\n }\n}","programmingLanguage":"Kotlin"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">val resumeToken = <span class="lg-highlight-title lg-highlight-class">BsonDocument</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx">val job = launch {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> val changeStream = collection.<span class="lg-highlight-title lg-highlight-function">watch</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .<span class="lg-highlight-title lg-highlight-function">resumeAfter</span>(resumeToken)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<span class="lg-highlight-property">collect</span> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">println</span>(it)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-154-menu" aria-describedby="select-154-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-155" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r5v:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r5v:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5v:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r5v:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_4vhe5e5b9" class="leafygreen-ui-38lglc" id="_eb7tu59d6" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
resume_after
</code>
modifier to resume
notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resume_after
</code>
modifier takes a value that must
resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
resume_token
</code>
in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"resume_token = cursor.resume_token\ncursor = db.inventory.watch(resume_after=resume_token)\ndocument = await cursor.next()","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">resume_token = cursor.<span class="lg-highlight-property">resume_token</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">cursor = db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">watch</span>(resume_after=resume_token)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-variable lg-highlight-language">document</span> = <span class="lg-highlight-keyword">await</span> cursor.<span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-157-menu" aria-describedby="select-157-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-158" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r60:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r60:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r60:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r60:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_yjt605yev" class="" id="_9xtfl2h72" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option to resume
notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option takes a value that must
resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"const collection = db.collection('inventory');\nconst changeStream = collection.watch();\n\nlet newChangeStream;\nchangeStream.once('change', next => {\n const resumeToken = changeStream.resumeToken;\n changeStream.close();\n\n newChangeStream = collection.watch([], { resumeAfter: resumeToken });\n newChangeStream.on('change', next => {\n processChange(next);\n });\n});","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> collection <!-- -->= <!-- -->db.<span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">'inventory'</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">const</span> changeStream <!-- -->= <!-- -->collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> newChangeStream<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">changeStream.<span class="lg-highlight-title lg-highlight-function">once</span>(<span class="lg-highlight-string">'change'</span>, <span class="lg-highlight-params lg-highlight-function">next</span><span class="lg-highlight-function"> =></span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">const</span> resumeToken <!-- -->= <!-- -->changeStream.<span class="lg-highlight-property">resumeToken</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->changeStream.<span class="lg-highlight-title lg-highlight-function">close</span>(<!-- -->)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->newChangeStream <!-- -->= <!-- -->collection.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->[<!-- -->]<!-- -->, <!-- -->{ <span class="lg-highlight-attr">resumeAfter</span>: <!-- -->resumeToken })<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->newChangeStream.<span class="lg-highlight-title lg-highlight-function">on</span>(<span class="lg-highlight-string">'change'</span>, <span class="lg-highlight-params lg-highlight-function">next</span><span class="lg-highlight-function"> =></span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">processChange</span>(<!-- -->next)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->})<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx">})<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-160-menu" aria-describedby="select-160-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-161" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r61:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r61:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r61:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r61:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_w4whx1ki4" class="leafygreen-ui-38lglc" id="_35peg4uwt" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option to resume
notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option takes a value that must
resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
$resumeToken
</code>
in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"$resumeToken = $changeStream->getResumeToken();\n\nif ($resumeToken === null) {\n throw new \\Exception('Resume token was not found');\n}\n\n$changeStream = $db->inventory->watch([], ['resumeAfter' => $resumeToken]);\n$changeStream->rewind();\n\n$firstChange = $changeStream->current();","programmingLanguage":"PHP"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">$resumeToken = $changeStream-><!-- --><span class="lg-highlight-title lg-highlight-function">getResumeToken</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">if</span> ($resumeToken === <span class="lg-highlight-literal">null</span>) {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">throw</span> <span class="lg-highlight-keyword">new</span> \<span class="lg-highlight-title lg-highlight-class">Exception</span>(<span class="lg-highlight-string">'Resume token was not found'</span>);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream = $db->inventory-<!-- -->><span class="lg-highlight-title lg-highlight-function">watch</span>([], [<span class="lg-highlight-string">'resumeAfter'</span> => $resumeToken]);</td></tr><tr class=""><td class="leafygreen-ui-7razhx">$changeStream-<!-- -->><span class="lg-highlight-title lg-highlight-function">rewind</span>();</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">$firstChange = $changeStream-><!-- --><span class="lg-highlight-title lg-highlight-function">current</span>();</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-163-menu" aria-describedby="select-163-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-164" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r62:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r62:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r62:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r62:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_mtnkryeo2" class="leafygreen-ui-38lglc" id="_b9bno9gcs" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
resume_after
</code>
modifier to resume
notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resume_after
</code>
modifier takes a value that must
resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
resume_token
</code>
in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"resume_token = cursor.resume_token\ncursor = db.inventory.watch(resume_after=resume_token)\nnext(cursor)","programmingLanguage":"Python"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">resume_token = cursor.<span class="lg-highlight-property">resume_token</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">cursor = db.<span class="lg-highlight-property">inventory</span>.<span class="lg-highlight-title lg-highlight-function">watch</span>(resume_after=resume_token)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-function">next</span>(cursor)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-166-menu" aria-describedby="select-166-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-167" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r63:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r63:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r63:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r63:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_cn4yno0z7" class="leafygreen-ui-38lglc" id="_28wdidehv" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
resume_after
</code>
modifier to resume
notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resume_after
</code>
modifier takes a value that must
resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
resume_token
</code>
in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\n change_stream = inventory.watch\n cursor = change_stream.to_enum\n next_change = cursor.next\n resume_token = change_stream.resume_token\n\n new_cursor = inventory.watch([], resume_after: resume_token).to_enum\n resumed_change = new_cursor.next\n","programmingLanguage":"Ruby"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> change_stream <!-- -->= <!-- -->inventory.<span class="lg-highlight-property">watch</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> cursor <!-- -->= <!-- -->change_stream.<span class="lg-highlight-property">to_enum</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> next_change <!-- -->= <!-- -->cursor.<span class="lg-highlight-property">next</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> resume_token <!-- -->= <!-- -->change_stream.<span class="lg-highlight-property">resume_token</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> new_cursor <!-- -->= <!-- -->inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- -->[<!-- -->], <span class="lg-highlight-attr">resume_after</span>: resume_token).<span class="lg-highlight-property">to_enum</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> resumed_change <!-- -->= <!-- -->new_cursor.<span class="lg-highlight-property">next</span></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-169-menu" aria-describedby="select-169-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-170" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r64:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r64:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r64:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r64:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_uvyf64uon" class="leafygreen-ui-38lglc" id="_t1mvcjib6" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option to resume
notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option takes a value that must
resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let inventory = db.collection(\"inventory\")\n\ninventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))\n .flatMap { changeStream in\n changeStream.next().map { _ in\n changeStream.resumeToken\n }.always { _ in\n _ = changeStream.kill()\n }\n }.flatMap { resumeToken in\n inventory.watch(options: ChangeStreamOptions(resumeAfter: resumeToken)).flatMap { newStream in\n newStream.forEach { event in\n // process event\n print(event)\n }\n }\n }","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> inventory = db.<!-- --><span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">"inventory"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- --><!-- --><span class="lg-highlight-attr">options</span>: <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span>(<!-- --><!-- --><span class="lg-highlight-attr">fullDocument</span>: .<span class="lg-highlight-property">updateLookup</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> .<span class="lg-highlight-property">flatMap</span> { changeStream <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">next</span>().<span class="lg-highlight-property">map</span> { _ <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> changeStream.<span class="lg-highlight-property">resumeToken</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }.<span class="lg-highlight-property">always</span> { _ <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> _ = changeStream.<span class="lg-highlight-title lg-highlight-function">kill</span>(<!-- -->)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }.<span class="lg-highlight-property">flatMap</span> { resumeToken <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> inventory.<!-- --><!-- --><span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- --><!-- --><span class="lg-highlight-attr">options</span>: <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span>(<!-- --><!-- --><span class="lg-highlight-attr">resumeAfter</span>: <!-- -->resumeToken)).<span class="lg-highlight-property">flatMap</span> { newStream <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> newStream.<!-- --><!-- --><span class="lg-highlight-property">forEach</span> { event <span class="lg-highlight-keyword">in</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// process event</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(event)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-172-menu" aria-describedby="select-172-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-173" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r65:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r65:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r65:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r65:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_wf66ytl4l" class="leafygreen-ui-38lglc" id="_lfjyh0ey2" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
You can use the
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option to resume
notifications after the operation specified in the resume
token. The
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
option takes a value that must
resolve to a resume token, e.g.
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
in the
example below.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"let inventory = db.collection(\"inventory\")\nlet changeStream = try inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))\nlet next = changeStream.next()\n\nlet resumeToken = changeStream.resumeToken\nlet resumedChangeStream = try inventory.watch(options: ChangeStreamOptions(resumeAfter: resumeToken))\nlet nextAfterResume = resumedChangeStream.next()","programmingLanguage":"Swift"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-9lvdmr">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> inventory = db.<!-- --><span class="lg-highlight-title lg-highlight-function">collection</span>(<span class="lg-highlight-string">"inventory"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> changeStream <!-- -->= <span class="lg-highlight-keyword">try</span> inventory.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- --><!-- --><span class="lg-highlight-attr">options</span>: <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span>(<!-- --><!-- --><span class="lg-highlight-attr">fullDocument</span>: <!-- -->.<span class="lg-highlight-property">updateLookup</span>))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> next = changeStream.<!-- --><span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> resumeToken = changeStream.<span class="lg-highlight-property">resumeToken</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> resumedChangeStream <!-- -->= <span class="lg-highlight-keyword">try</span> inventory.<span class="lg-highlight-title lg-highlight-function">watch</span>(<!-- --><!-- --><span class="lg-highlight-attr">options</span>: <span class="lg-highlight-title lg-highlight-class">ChangeStreamOptions</span>(<!-- --><!-- --><span class="lg-highlight-attr">resumeAfter</span>: <!-- -->resumeToken))</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">let</span> nextAfterResume = resumedChangeStream.<!-- --><span class="lg-highlight-title lg-highlight-function">next</span>()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-hcrzu6" data-testid="leafygreen-code-panel">
<div class="leafygreen-ui-148abs6">
<div class="leafygreen-ui-omjz13" data-lgid="lg-select">
<button aria-controls="select-175-menu" aria-describedby="select-175-description" aria-disabled="false" aria-expanded="false" aria-invalid="false" aria-labelledby="Language Picker" class="lg-ui-button-0000 leafygreen-ui-dvfj9v" data-lgid="lg-button" data-testid="leafygreen-ui-select-menubutton" id="select-176" type="button" value="Node.js">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg aria-hidden="true" class="leafygreen-ui-18si895" fill="none" role="presentation" viewbox="0 0 30 30" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2131_12968)">
<mask height="19" id="mask0_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask0_2131_12968)">
<mask height="19" id="mask1_2131_12968" maskunits="userSpaceOnUse" width="30" x="0" y="6">
<path d="M30 6.04395H0V24.3825H30V6.04395Z" fill="white">
</path>
</mask>
<g mask="url(#mask1_2131_12968)">
<path d="M14.8241 24.3823C14.7224 24.3823 14.6274 24.3552 14.5393 24.3078L13.6374 23.7731C13.5018 23.6986 13.5696 23.6715 13.6103 23.658C13.7934 23.597 13.8273 23.5835 14.0172 23.4751C14.0375 23.4616 14.0646 23.4684 14.085 23.4819L14.7767 23.8949C14.8038 23.9084 14.8377 23.9084 14.858 23.8949L17.5637 22.3311C17.5909 22.3176 17.6044 22.2905 17.6044 22.2567V19.136C17.6044 19.1021 17.5909 19.075 17.5637 19.0615L14.858 17.5045C14.8309 17.4909 14.797 17.4909 14.7767 17.5045L12.071 19.0615C12.0439 19.075 12.0302 19.1089 12.0302 19.136V22.2567C12.0302 22.2838 12.0439 22.3176 12.071 22.3311L12.8101 22.7576C13.2102 22.9607 13.4611 22.7237 13.4611 22.4868V19.4067C13.4611 19.3661 13.495 19.3255 13.5425 19.3255H13.8884C13.929 19.3255 13.9697 19.3593 13.9697 19.4067V22.4868C13.9697 23.0217 13.6781 23.333 13.1695 23.333C13.0136 23.333 12.8915 23.333 12.5457 23.1638L11.8336 22.7576C11.6574 22.6561 11.5488 22.4665 11.5488 22.2635V19.1427C11.5488 18.9396 11.6574 18.7501 11.8336 18.6485L14.5393 17.0848C14.7089 16.99 14.9394 16.99 15.109 17.0848L17.8146 18.6485C17.9909 18.7501 18.0995 18.9396 18.0995 19.1427V22.2635C18.0995 22.4665 17.9909 22.6561 17.8146 22.7576L15.109 24.3214C15.0276 24.3552 14.9259 24.3823 14.8241 24.3823Z" fill="black">
</path>
<path d="M15.6643 22.2362C14.4775 22.2362 14.2334 21.6946 14.2334 21.2343C14.2334 21.1936 14.2673 21.153 14.3147 21.153H14.6674C14.7081 21.153 14.742 21.1801 14.742 21.2207C14.7963 21.5795 14.9522 21.7555 15.671 21.7555C16.2407 21.7555 16.4847 21.6269 16.4847 21.3223C16.4847 21.1462 16.4169 21.0177 15.5286 20.9297C14.7895 20.8552 14.3284 20.6928 14.3284 20.1038C14.3284 19.5555 14.7895 19.2305 15.5625 19.2305C16.4305 19.2305 16.8577 19.5284 16.912 20.1782C16.912 20.1986 16.9052 20.2189 16.8916 20.2392C16.8781 20.2527 16.8577 20.2663 16.8374 20.2663H16.4779C16.4441 20.2663 16.4101 20.2392 16.4034 20.2053C16.322 19.833 16.1118 19.7111 15.5557 19.7111C14.9319 19.7111 14.8573 19.9278 14.8573 20.0902C14.8573 20.2866 14.9454 20.3475 15.7863 20.4558C16.6204 20.5641 17.0137 20.7198 17.0137 21.302C17.0137 21.8977 16.5187 22.2362 15.6643 22.2362Z" fill="black">
</path>
<path d="M19.5706 18.9597C19.5706 19.244 19.3333 19.481 19.0485 19.481C18.7636 19.481 18.5264 19.2507 18.5264 18.9597C18.5264 18.6618 18.7704 18.4385 19.0485 18.4385C19.3265 18.4385 19.5706 18.6686 19.5706 18.9597ZM18.6077 18.9597C18.6077 19.2034 18.8044 19.3997 19.0417 19.3997C19.2859 19.3997 19.4825 19.1967 19.4825 18.9597C19.4825 18.716 19.2859 18.5264 19.0417 18.5264C18.8111 18.5264 18.6077 18.716 18.6077 18.9597ZM18.8519 18.6686H19.0553C19.1231 18.6686 19.2587 18.6686 19.2587 18.8243C19.2587 18.9326 19.1909 18.9529 19.1502 18.9664C19.2316 18.9732 19.2384 19.0274 19.2451 19.1019C19.2519 19.1493 19.2587 19.2304 19.2722 19.2575H19.1502C19.1502 19.2304 19.1298 19.0815 19.1298 19.0748C19.1231 19.0409 19.1095 19.0274 19.0688 19.0274H18.9672V19.2643H18.8519V18.6686ZM18.9603 18.9258H19.0485C19.1231 18.9258 19.1366 18.8716 19.1366 18.8446C19.1366 18.7634 19.0824 18.7634 19.0485 18.7634H18.9535V18.9258H18.9603Z" fill="black">
</path>
<path clip-rule="evenodd" d="M6.43536 12.1766C6.43536 12.0548 6.36754 11.9397 6.25904 11.8788L3.39059 10.2202C3.34313 10.1932 3.28887 10.1796 3.23463 10.1729C3.22785 10.1729 3.20751 10.1729 3.20751 10.1729C3.15325 10.1729 3.09901 10.1932 3.05154 10.2202L0.176311 11.872C0.067812 11.9329 0 12.048 0 12.1766L0.00678119 16.6174C0.00678119 16.6783 0.0406871 16.7393 0.0949364 16.7663C0.149187 16.8002 0.216998 16.8002 0.264466 16.7663L1.97332 15.7915C2.08183 15.7306 2.14964 15.6155 2.14964 15.4936V13.4154C2.14964 13.2936 2.21745 13.1785 2.32595 13.1176L3.05154 12.6979C3.10579 12.664 3.16682 12.6505 3.22785 12.6505C3.28887 12.6505 3.34991 12.664 3.39738 12.6979L4.12297 13.1176C4.23147 13.1785 4.29927 13.2936 4.29927 13.4154V15.4936C4.29927 15.6155 4.36709 15.7306 4.47559 15.7915L6.17089 16.7663C6.22513 16.8002 6.29295 16.8002 6.3472 16.7663C6.40144 16.7393 6.43536 16.6783 6.43536 16.6174V12.1766Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M20.2551 6.06425C20.201 6.03718 20.1332 6.03718 20.0857 6.06425C20.0314 6.0981 19.9975 6.15226 19.9975 6.21318V10.6133C19.9975 10.654 19.9771 10.6946 19.9364 10.7217C19.8958 10.742 19.8551 10.742 19.8144 10.7217L19.0956 10.3087C18.9871 10.2478 18.8582 10.2478 18.7498 10.3087L15.8746 11.9673C15.766 12.0282 15.6982 12.1433 15.6982 12.2651V15.5754C15.6982 15.6973 15.766 15.8123 15.8746 15.8732L18.7498 17.5318C18.8582 17.5927 18.9871 17.5927 19.0956 17.5318L21.9708 15.8732C22.0793 15.8123 22.1471 15.6973 22.1471 15.5754V7.32338C22.1471 7.19476 22.0793 7.07968 21.9708 7.01875L20.2551 6.06425ZM19.9907 14.4855C19.9907 14.5194 19.9771 14.5464 19.9501 14.56L18.9667 15.1286C18.9397 15.1421 18.9057 15.1421 18.8786 15.1286L17.8953 14.56C17.8682 14.5464 17.8547 14.5126 17.8547 14.4855V13.3482C17.8547 13.3144 17.8682 13.2873 17.8953 13.2738L18.8786 12.7051C18.9057 12.6916 18.9397 12.6916 18.9667 12.7051L19.9501 13.2738C19.9771 13.2873 19.9907 13.3212 19.9907 13.3482V14.4855Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M29.8235 13.3214C29.932 13.2605 29.9931 13.1454 29.9931 13.0235V12.218C29.9931 12.0961 29.9253 11.981 29.8235 11.9201L26.9687 10.2683C26.8602 10.2074 26.7314 10.2074 26.6228 10.2683L23.7476 11.9269C23.6391 11.9878 23.5713 12.1029 23.5713 12.2247V15.535C23.5713 15.6569 23.6391 15.772 23.7476 15.8328L26.6025 17.4575C26.7109 17.5185 26.8398 17.5185 26.9415 17.4575L28.6707 16.4963C28.725 16.4692 28.7589 16.4083 28.7589 16.3474C28.7589 16.2864 28.725 16.2255 28.6707 16.1984L25.782 14.5399C25.7277 14.506 25.6938 14.4519 25.6938 14.391V13.3552C25.6938 13.2943 25.7277 13.2334 25.782 13.2063L26.6839 12.6918C26.7381 12.658 26.8059 12.658 26.8602 12.6918L27.7621 13.2063C27.8163 13.2402 27.8502 13.2943 27.8502 13.3552V14.1676C27.8502 14.2285 27.8841 14.2894 27.9384 14.3165C27.9927 14.3504 28.0605 14.3504 28.1147 14.3165L29.8235 13.3214Z" fill="black" fill-rule="evenodd">
</path>
<path clip-rule="evenodd" d="M26.7521 13.1654C26.7724 13.1519 26.7996 13.1519 26.8199 13.1654L27.3692 13.4836C27.3895 13.4971 27.4031 13.5174 27.4031 13.5445V14.1809C27.4031 14.2079 27.3895 14.2282 27.3692 14.2418L26.8199 14.5599C26.7996 14.5735 26.7724 14.5735 26.7521 14.5599L26.2028 14.2418C26.1825 14.2282 26.1689 14.2079 26.1689 14.1809V13.5445C26.1689 13.5174 26.1825 13.4971 26.2028 13.4836L26.7521 13.1654Z" fill="#5FA04E" fill-rule="evenodd">
</path>
<path d="M11.2331 10.2292C11.1227 10.1671 10.9917 10.1671 10.8814 10.2292L7.97807 11.9053C7.86772 11.9673 7.80566 12.0846 7.80566 12.2088V15.5679C7.80566 15.692 7.87462 15.8092 7.97807 15.8714L10.8814 17.5474C10.9917 17.6095 11.1227 17.6095 11.2331 17.5474L14.1364 15.8714C14.2467 15.8092 14.3088 15.692 14.3088 15.5679V12.2088C14.3088 12.0846 14.2398 11.9673 14.1364 11.9053L11.2331 10.2292Z" fill="black">
</path>
<path d="M14.1432 11.9059L11.2261 10.2298C11.1985 10.216 11.164 10.2022 11.1365 10.1953L7.875 15.7825C7.90258 15.817 7.94609 15.8673 7.98056 15.888L10.8813 17.548C10.9641 17.5963 11.0606 17.6101 11.1503 17.5825L14.219 11.968C14.1983 11.9404 14.1708 11.9197 14.1432 11.9059Z" fill="black">
</path>
<path d="M14.136 15.8715C14.2187 15.8231 14.2933 15.7327 14.2933 15.6236L11.1154 10.1879C11.0326 10.1741 10.943 10.181 10.8671 10.2293L7.9707 11.8985L11.0947 17.5958C11.1361 17.5889 11.1843 17.5751 11.2257 17.5544L14.136 15.8715Z" fill="url(#:r66:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":r66:paint0" x1="12.2396" x2="9.61385" y1="11.4775" y2="16.8331">
<stop stop-color="#3F873F">
</stop>
<stop offset="0.3296" stop-color="#3F8B3D">
</stop>
<stop offset="0.6367" stop-color="#3E9638">
</stop>
<stop offset="0.9341" stop-color="#3DA92E">
</stop>
<stop offset="1" stop-color="#3DAE2B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r66:paint1" x1="10.6159" x2="17.9826" y1="14.2811" y2="8.83932">
<stop offset="0.1376" stop-color="#3F873F">
</stop>
<stop offset="0.4016" stop-color="#52A044">
</stop>
<stop offset="0.7129" stop-color="#64B749">
</stop>
<stop offset="0.9081" stop-color="#6ABF4B">
</stop>
</lineargradient>
<lineargradient gradientunits="userSpaceOnUse" id=":r66:paint2" x1="7.69907" x2="14.4172" y1="13.8874" y2="13.8874">
<stop offset="0.0919165" stop-color="#6ABF4B">
</stop>
<stop offset="0.2871" stop-color="#64B749">
</stop>
<stop offset="0.5984" stop-color="#52A044">
</stop>
<stop offset="0.8624" stop-color="#3F873F">
</stop>
</lineargradient>
<clippath id="clip0_2131_12968">
<rect fill="white" height="18.427" transform="translate(0 6)" width="30">
</rect>
</clippath>
</defs>
</svg>
<div class="leafygreen-ui-obh8q1">
<div class="lg-ui-select-menu-0000 leafygreen-ui-17aj16e" data-lgid="lg-select-button_text">
Node.js
</div>
</div>
<svg alt="" aria-hidden="true" class="leafygreen-ui-fn50nl" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M8.67903 10.7962C8.45271 11.0679 8.04729 11.0679 7.82097 10.7962L4.63962 6.97649C4.3213 6.59428 4.5824 6 5.06866 6L11.4313 6C11.9176 6 12.1787 6.59428 11.8604 6.97649L8.67903 10.7962Z" fill="currentColor">
</path>
</svg>
</div>
</button>
<div aria-live="polite" aria-relevant="all" class="leafygreen-ui-11ydt2g" data-lgid="lg-form_field-feedback" data-testid="lg-form_field-feedback">
</div>
</div>
</div>
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-stream-start-after">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
startAfter
</code>
for Change Streams
<a class="headerlink leafygreen-ui-1kepx47" href="#startafter-for-change-streams" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="startafter-for-change-streams">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can start a new change stream after a specific event by passing a resume
token to
<code class="leafygreen-ui-1l06pbn">
startAfter
</code>
when opening the cursor. Unlike
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-resume-after" target="_self">
<span>
resumeAfter
</span>
</a>
,
<code class="leafygreen-ui-1l06pbn">
startAfter
</code>
can
resume notifications after an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/invalidate/#std-label-change-event-invalidate">
invalidate event
</a>
by creating a new change stream.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-resume-token" target="_self">
<span>
Resume Tokens
</span>
</a>
for more information on the resume token.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The oplog must have enough history to locate the operation
associated with the token or the timestamp, if the timestamp is in
the past.
</p>
</li>
</ul>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-stream-resume-token">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Resume Tokens
<a class="headerlink leafygreen-ui-1kepx47" href="#resume-tokens" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resume-tokens">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The resume token is available from multiple sources:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:80%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Source
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-event-resume-token" target="_self">
<span>
Change Events
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Each change event notification includes a resume token
on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregate-resume-token" target="_self">
<span>
Aggregation
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/changeStream/#mongodb-pipeline-pipe.-changeStream">
<code class="leafygreen-ui-1l06pbn">
$changeStream
</code>
</a>
aggregation stage includes
a resume token on the
<code class="leafygreen-ui-1l06pbn">
cursor.postBatchResumeToken
</code>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
This field only appears when using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
command.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-xejatx" role="rowheader">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-getMore-resume-token" target="_self">
<span>
Get More
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
command includes a resume token on the
<code class="leafygreen-ui-1l06pbn">
cursor.postBatchResumeToken
</code>
field.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 4.2, change streams will throw an exception if
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-stream-modify-output" target="_self">
<span>
change stream aggregation pipeline
</span>
</a>
modifies an event's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-event-id">
_id
</a>
field.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
MongoDB provides a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/snippets/#std-label-snip-overview" target="_self">
<span>
"snippet"
</span>
</a>
, an
extension to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
, that decodes hex-encoded
resume tokens.
</p>
<p class="leafygreen-ui-1kp3ins">
You can install and run the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb-labs/mongosh-snippets/tree/main/snippets/resumetoken" rel="noopener noreferrer">
<span>
resumetoken
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
snippet from
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"snippet install resumetoken\ndecodeResumeToken('<resume>')","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">snippet install resumetoken</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-function">decodeResumeToken</span>(<span class="lg-highlight-string">'<RESUME TOKEN>'</span>)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You can also run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb-labs/mongosh-snippets/tree/main/snippets/resumetoken" rel="noopener noreferrer">
<span>
resumetoken
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
from the command line (without using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
) if
<code class="leafygreen-ui-1l06pbn">
npm
</code>
is installed on your system:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"npx mongodb-resumetoken-decoder <resume>","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">npx mongodb<!-- -->-<!-- -->resumetoken<!-- -->-<!-- -->decoder <!-- --><<span class="lg-highlight-variable lg-highlight-constant">RESUME</span> <span class="lg-highlight-variable lg-highlight-constant">TOKEN</span>></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See the following for more details on:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb-labs/mongosh-snippets/tree/main/snippets/resumetoken" rel="noopener noreferrer">
<span>
resumetoken
</span>
<svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
</path>
<path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
</path>
</svg>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/snippets/working-with-snippets/#std-label-snip-using-snippets" target="_self">
<span>
using snippets
</span>
</a>
in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
.
</span>
</a>
</p>
</li>
</ul>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-event-resume-token">
</span>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Resume Tokens from Change Events
<a class="headerlink leafygreen-ui-1kepx47" href="#resume-tokens-from-change-events" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resume-tokens-from-change-events">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Change event notifications include a resume token on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\": {\n \"_data\": \"82635019A0000000012B042C0100296E5A1004AB1154ACACD849A48C61756D70D3B21F463C6F7065726174696F6E54797065003C696E736572740046646F63756D656E744B65790046645F69640064635019A078BE67426D7CF4D2000004\"\n },\n \"operationType\": \"insert\",\n \"clusterTime\": Timestamp({ \"t\": 1666193824, \"i\": 1 }),\n \"collectionUUID\": new UUID(\"ab1154ac-acd8-49a4-8c61-756d70d3b21f\"),\n \"wallTime\": ISODate(\"2022-10-19T15:37:04.604Z\"),\n \"fullDocument\": {\n \"_id\": ObjectId(\"635019a078be67426d7cf4d2\"'),\n \"name\": \"Giovanni Verga\"\n },\n \"ns\": {\n \"db\": \"test\",\n \"coll\": \"names\"\n },\n \"documentKey\": {\n \"_id\": ObjectId(\"635019a078be67426d7cf4d2\")\n }\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"_id"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"_data"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"82635019A0000000012B042C0100296E5A1004AB1154ACACD849A48C61756D70D3B21F463C6F7065726174696F6E54797065003C696E736572740046646F63756D656E744B65790046645F69640064635019A078BE67426D7CF4D2000004"</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"operationType"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"insert"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"clusterTime"</span><span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-punctuation">{</span> <span class="lg-highlight-attr">"t"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1666193824</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-attr">"i"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span> <span class="lg-highlight-punctuation">}</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"collectionUUID"</span><span class="lg-highlight-punctuation">:</span> new UUID(<span class="lg-highlight-string">"ab1154ac-acd8-49a4-8c61-756d70d3b21f"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"wallTime"</span><span class="lg-highlight-punctuation">:</span> ISODate(<span class="lg-highlight-string">"2022-10-19T15:37:04.604Z"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"fullDocument"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"_id"</span><span class="lg-highlight-punctuation">:</span> ObjectId(<span class="lg-highlight-string">"635019a078be67426d7cf4d2"</span>')<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"name"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"Giovanni Verga"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"ns"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"db"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test"</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"coll"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"names"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"documentKey"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"_id"</span><span class="lg-highlight-punctuation">:</span> ObjectId(<span class="lg-highlight-string">"635019a078be67426d7cf4d2"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-aggregate-resume-token">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Resume Tokens from
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#resume-tokens-from-aggregate" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resume-tokens-from-aggregate">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
When using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
command, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/changeStream/#mongodb-pipeline-pipe.-changeStream">
<code class="leafygreen-ui-1l06pbn">
$changeStream
</code>
</a>
aggregation stage includes a resume token on the
<code class="leafygreen-ui-1l06pbn">
cursor.postBatchResumeToken
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"cursor\": {\n \"firstBatch\": [],\n \"postBatchResumeToken\": {\n \"_data\": \"8263515EAC000000022B0429296E1404\"\n },\n \"id\": Long(\"4309380460777152828\"),\n \"ns\": \"test.names\"\n },\n \"ok\": 1,\n \"$clusterTime\": {\n \"clusterTime\": Timestamp({ \"t\": 1666277036, \"i\": 1 }),\n \"signature\": {\n \"hash\": Binary(Buffer.from(\"0000000000000000000000000000000000000000\", \"hex\"), 0),\n \"keyId\": Long(\"0\")\n }\n },\n \"operationTime\": Timestamp({ \"t\": 1666277036, \"i\": 1 })\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"cursor"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"firstBatch"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span><span class="lg-highlight-punctuation">]</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"postBatchResumeToken"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"_data"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"8263515EAC000000022B0429296E1404"</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"id"</span><span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"4309380460777152828"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"ns"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.names"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"ok"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"$clusterTime"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"clusterTime"</span><span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-punctuation">{</span> <span class="lg-highlight-attr">"t"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1666277036</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-attr">"i"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span> <span class="lg-highlight-punctuation">}</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"signature"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"hash"</span><span class="lg-highlight-punctuation">:</span> Binary(Buffer.from(<span class="lg-highlight-string">"0000000000000000000000000000000000000000"</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-string">"hex"</span>)<span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"keyId"</span><span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"0"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"operationTime"</span><span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-punctuation">{</span> <span class="lg-highlight-attr">"t"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1666277036</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-attr">"i"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span> <span class="lg-highlight-punctuation">}</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-getMore-resume-token">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Resume Tokens from
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#resume-tokens-from-getmore" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resume-tokens-from-getmore">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
command also includes a resume token on the
<code class="leafygreen-ui-1l06pbn">
cursor.postBatchResumeToken
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"cursor\": {\n \"nextBatch\": [],\n \"postBatchResumeToken\": {\n \"_data\": \"8263515979000000022B0429296E1404\"\n },\n \"id\": Long(\"7049907285270685005\"),\n \"ns\": \"test.names\"\n },\n \"ok\": 1,\n \"$clusterTime\": {\n \"clusterTime\": Timestamp( { \"t\": 1666275705, \"i\": 1 } ),\n \"signature\": {\n \"hash\": Binary(Buffer.from(\"0000000000000000000000000000000000000000\", \"hex\"), 0),\n \"keyId\": Long(\"0\")\n }\n },\n \"operationTime\": Timestamp({ \"t\": 1666275705, \"i\": 1 })\n}","programmingLanguage":"JSON"}
</script>
<div class="css-1lju8ps">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-tyg50d">
<pre class="leafygreen-ui-1453az5" tabindex="0"><code class="lg-highlight-hljs-light json leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"cursor"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"nextBatch"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">[</span><span class="lg-highlight-punctuation">]</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"postBatchResumeToken"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"_data"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"8263515979000000022B0429296E1404"</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"id"</span><span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"7049907285270685005"</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"ns"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-string">"test.names"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"ok"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"$clusterTime"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"clusterTime"</span><span class="lg-highlight-punctuation">:</span> Timestamp( <span class="lg-highlight-punctuation">{</span> <span class="lg-highlight-attr">"t"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1666275705</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-attr">"i"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span> <span class="lg-highlight-punctuation">}</span> )<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"signature"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"hash"</span><span class="lg-highlight-punctuation">:</span> Binary(Buffer.from(<span class="lg-highlight-string">"0000000000000000000000000000000000000000"</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-string">"hex"</span>)<span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-number">0</span>)<span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"keyId"</span><span class="lg-highlight-punctuation">:</span> Long(<span class="lg-highlight-string">"0"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-punctuation">}</span><span class="lg-highlight-punctuation">,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">"operationTime"</span><span class="lg-highlight-punctuation">:</span> Timestamp(<span class="lg-highlight-punctuation">{</span> <span class="lg-highlight-attr">"t"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1666275705</span><span class="lg-highlight-punctuation">,</span> <span class="lg-highlight-attr">"i"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-number">1</span> <span class="lg-highlight-punctuation">}</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-punctuation">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Use Cases
<a class="headerlink leafygreen-ui-1kepx47" href="#use-cases" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="use-cases">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Change streams can benefit architectures with reliant business systems,
informing downstream systems once data changes are durable. For example,
change streams can save time for developers when implementing Extract,
Transform, and Load (ETL) services, cross-platform synchronization,
collaboration functionality, and notification services.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-streams-access">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Access Control
<a class="headerlink leafygreen-ui-1kepx47" href="#access-control" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="access-control">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For deployments enforcing
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authentication/#std-label-authentication">
Authentication on Self-Managed Deployments
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-authorization">
authorization
<!-- -->
:
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To open a change stream against specific collection, applications
must have privileges that grant
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-changeStream">
<code class="leafygreen-ui-1l06pbn">
changeStream
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
actions on the corresponding collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ resource: { db: <dbname>, collection: <collection> }, actions: [ \"find\", \"changeStream\" ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <<!-- -->dbname<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">collection</span>: <<!-- -->collection<!-- -->> <!-- -->}<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span>, <span class="lg-highlight-string">"changeStream"</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To open a change stream on a single database, applications must have
privileges that grant
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-changeStream">
<code class="leafygreen-ui-1l06pbn">
changeStream
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
actions on all non-
<code class="leafygreen-ui-1l06pbn">
system
</code>
collections in the
database.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ resource: { db: <dbname>, collection: \"\" }, actions: [ \"find\", \"changeStream\" ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <<!-- -->dbname<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">""</span> }<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span>, <span class="lg-highlight-string">"changeStream"</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To open a change stream on an entire deployment, applications must
have privileges that grant
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-changeStream">
<code class="leafygreen-ui-1l06pbn">
changeStream
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
actions on all non-
<code class="leafygreen-ui-1l06pbn">
system
</code>
collections for all
databases in the deployment.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ resource: { db: \"\", collection: \"\" }, actions: [ \"find\", \"changeStream\" ] }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">resource</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">""</span>, <span class="lg-highlight-attr">collection</span>: <span class="lg-highlight-string">""</span> }<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span>, <span class="lg-highlight-string">"changeStream"</span> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-streams-event-notification">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Event Notification
<a class="headerlink leafygreen-ui-1kepx47" href="#event-notification" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="event-notification">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Change streams only notify on data changes that have persisted to a majority
of data-bearing members in the replica set. This ensures that notifications are
triggered only by majority-committed changes that are durable in failure
scenarios.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider a 3-member
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
</a>
with a change stream
cursor opened against the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
. If a client issues an insert
operation, the change stream only notifies the application of the data change
once that insert has persisted to a majority of data-bearing members.
</p>
<p class="leafygreen-ui-1kp3ins">
If an operation is associated with a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/">
transaction
</a>
, the change event document includes the
<code class="leafygreen-ui-1l06pbn">
txnNumber
</code>
and the
<code class="leafygreen-ui-1l06pbn">
lsid
</code>
.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Collation
<a class="headerlink leafygreen-ui-1kepx47" href="#collation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="collation">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Change streams use
<code class="leafygreen-ui-1l06pbn">
simple
</code>
binary comparisons unless an explicit collation is
provided.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Change Streams and Orphan Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#change-streams-and-orphan-documents" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="change-streams-and-orphan-documents">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.3, during
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-range-migration-procedure">
range migration
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-changeStreams" target="_self">
<span>
change stream
</span>
</a>
events are not generated for updates to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-orphaned-document">
orphaned documents
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-stream-pre-post-images">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Change Streams with Document Pre- and Post-Images
<a class="headerlink leafygreen-ui-1kepx47" href="#change-streams-with-document-pre--and-post-images" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="change-streams-with-document-pre--and-post-images">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, you can use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream events
</a>
to output the version of a document before and
after changes (the document pre- and post-images):
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The pre-image is the document before it was replaced, updated, or
deleted. There is no pre-image for an inserted document.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The post-image is the document after it was inserted, replaced, or
updated. There is no post-image for a deleted document.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Enable
<code class="leafygreen-ui-1l06pbn">
changeStreamPreAndPostImages
</code>
for a collection using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.createCollection/#mongodb-method-db.createCollection">
<code class="leafygreen-ui-1l06pbn">
db.createCollection()
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/create/#mongodb-dbcommand-dbcmd.create">
<code class="leafygreen-ui-1l06pbn">
create
</code>
</a>
, or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collMod/#mongodb-dbcommand-dbcmd.collMod">
<code class="leafygreen-ui-1l06pbn">
collMod
</code>
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Pre- and post-images are not available for a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream event
</a>
if the images were:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Not enabled on the collection at the time of a document update or
delete operation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Removed after the pre- and post-image retention time set in
<code class="leafygreen-ui-1l06pbn">
expireAfterSeconds
</code>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The following example sets
<code class="leafygreen-ui-1l06pbn">
expireAfterSeconds
</code>
to
<code class="leafygreen-ui-1l06pbn">
100
</code>
seconds on an entire cluster:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use admin\ndb.runCommand( {\n setClusterParameter:\n { changeStreamOptions: {\n preAndPostImages: { expireAfterSeconds: 100 }\n } }\n} )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use admin</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">setClusterParameter</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">changeStreamOptions</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">preAndPostImages</span>: <!-- -->{ <span class="lg-highlight-attr">expireAfterSeconds</span>: <span class="lg-highlight-number">100</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The following example returns the current
<code class="leafygreen-ui-1l06pbn">
changeStreamOptions
</code>
settings, including
<code class="leafygreen-ui-1l06pbn">
expireAfterSeconds
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { getClusterParameter: \"changeStreamOptions\" } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">adminCommand</span>( <!-- -->{ <span class="lg-highlight-attr">getClusterParameter</span>: <span class="lg-highlight-string">"changeStreamOptions"</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Setting
<code class="leafygreen-ui-1l06pbn">
expireAfterSeconds
</code>
to
<code class="leafygreen-ui-1l06pbn">
off
</code>
uses the default retention
policy: pre- and post-images are retained until the corresponding
change stream events are removed from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If a change stream event is removed from the oplog, then the
corresponding pre- and post-images are also deleted regardless of
the
<code class="leafygreen-ui-1l06pbn">
expireAfterSeconds
</code>
pre- and post-image retention time.
</p>
</li>
</ul>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Additional considerations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Enabling pre- and post-images consumes storage space and adds
processing time. Only enable pre- and post-images if you need them.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Limit the change stream event size to less than 16 megabytes. To limit
the event size, you can:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Limit the document size to 8 megabytes. You can request pre- and
post-images simultaneously in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change stream output
</a>
if other change stream event fields like
<code class="leafygreen-ui-1l06pbn">
updateDescription
</code>
are not large.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Request only post-images in the change stream output for documents
up to 16 megabytes if other change stream event fields like
<code class="leafygreen-ui-1l06pbn">
updateDescription
</code>
are not large.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Request only pre-images in the change stream output for documents up
to 16 megabytes if:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
document updates affect only a small fraction of the document
structure or content,
<em>
and
</em>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
do not cause a
<code class="leafygreen-ui-1l06pbn">
replace
</code>
change event. A
<code class="leafygreen-ui-1l06pbn">
replace
</code>
event
always includes the post-image.
</p>
</li>
</ul>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To request a pre-image, you set
<code class="leafygreen-ui-1l06pbn">
fullDocumentBeforeChange
</code>
to
<code class="leafygreen-ui-1l06pbn">
required
</code>
or
<code class="leafygreen-ui-1l06pbn">
whenAvailable
</code>
in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
db.collection.watch()
</code>
</a>
.
To request a post-image, you set
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
using the same
method.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Pre-images are written to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-collections/#mongodb-data-config.system.preimages">
<code class="leafygreen-ui-1l06pbn">
config.system.preimages
</code>
</a>
collection.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
config.system.preimages
</code>
collection may become large. To
limit the collection size, you can set
<code class="leafygreen-ui-1l06pbn">
expireAfterSeconds
</code>
time for the pre-images as shown earlier.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Pre-images are removed asynchronously by a background process.
</p>
</li>
</ul>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Backward-Incompatible Feature
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, if you are using document pre- and post-images
for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
change streams
</a>
, you must disable
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collMod/#std-label-collMod-change-stream-pre-and-post-images">
changeStreamPreAndPostImages
</a>
for each collection using
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collMod/#mongodb-dbcommand-dbcmd.collMod">
<code class="leafygreen-ui-1l06pbn">
collMod
</code>
</a>
command before you can downgrade to an earlier
MongoDB version.
</p>
</div>
</div>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For change stream events and output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
Change Events
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To watch a collection for changes, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
db.collection.watch()
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For complete examples with the change stream output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#std-label-db.collection.watch-change-streams-pre-and-post-images-example">
Change Streams with Document Pre- and Post-Images
<!-- -->
.
</a>
</p>
</li>
</ul>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For complete examples with the change stream output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.watch/#std-label-db.collection.watch-change-streams-pre-and-post-images-example">
Change Streams with Document Pre- and Post-Images
<!-- -->
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/cursor.pretty/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
cursor.pretty()
<a class="headerlink leafygreen-ui-1kepx47" href="#cursor.pretty--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="cursor.pretty--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
cursor.pretty()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-cursor.pretty" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-cursor.pretty">
</div>
</a>
</dt>
<dd>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for a language-specific driver, such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Configures the cursor to display results in a format that
is easy to read.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.pretty" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
pretty()
</code>
</span>
</a>
method has the following prototype form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.find(<query>).pretty()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<<!-- -->query<!-- -->><!-- -->).<span class="lg-highlight-title lg-highlight-function">pretty</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.pretty" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
pretty()
</code>
</span>
</a>
method:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Does not change the output format in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Changes the output format in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongo/#std-label-mongo">
legacy mongo shell
<!-- -->
.
</a>
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Consider the following document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.books.insertOne({\n \"_id\" : ObjectId(\"54f612b6029b47909a90ce8d\"),\n \"title\" : \"A Tale of Two Cities\",\n \"text\" : \"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness...\",\n \"authorship\" : \"Charles Dickens\"})","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">books</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"54f612b6029b47909a90ce8d"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"A Tale of Two Cities"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"text"</span> <!-- -->: <span class="lg-highlight-string">"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness..."</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"authorship"</span> <!-- -->: <span class="lg-highlight-string">"Charles Dickens"</span>})</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
By default,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
returns data in a dense format:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.books.find()\n{ \"_id\" : ObjectId(\"54f612b6029b47909a90ce8d\"), \"title\" : \"A Tale of Two Cities\", \"text\" : \"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness...\", \"authorship\" : \"Charles Dickens\" }","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">books</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"54f612b6029b47909a90ce8d"</span>)<!-- -->, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"A Tale of Two Cities"</span>, <span class="lg-highlight-string">"text"</span> <!-- -->: <span class="lg-highlight-string">"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness..."</span>, <span class="lg-highlight-string">"authorship"</span> <!-- -->: <span class="lg-highlight-string">"Charles Dickens"</span> }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
By using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.pretty" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.pretty()
</code>
</span>
</a>
you can set the cursor to return data in a
format that is easier to read:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.books.find().pretty()\n{\n \"_id\" : ObjectId(\"54f612b6029b47909a90ce8d\"),\n \"title\" : \"A Tale of Two Cities\",\n \"text\" : \"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness...\",\n \"authorship\" : \"Charles Dickens\"\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">books</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">pretty</span>(<!-- -->)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"54f612b6029b47909a90ce8d"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"A Tale of Two Cities"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"text"</span> <!-- -->: <span class="lg-highlight-string">"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness..."</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"authorship"</span> <!-- -->: <span class="lg-highlight-string">"Charles Dickens"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/convertShardKeyToHashed/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
convertShardKeyToHashed
<a class="headerlink leafygreen-ui-1kepx47" href="#convertshardkeytohashed" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="convertshardkeytohashed">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#description" target="_self">
<span>
Description
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#example" target="_self">
<span>
Example
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Description
<a class="headerlink leafygreen-ui-1kepx47" href="#description" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="description">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
convertShardKeyToHashed(<Object>)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-convertShardKeyToHashed" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-convertShardKeyToHashed">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the hashed value for the input. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-convertShardKeyToHashed" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
convertShardKeyToHashed()
</code>
</span>
</a>
method uses the same hashing
function as the hashed index and can be used to see what the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/hashed-sharding/#std-label-sharding-hashed">
hashed value
</a>
would be for a key.
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Consider a sharded collection that uses a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/hashed-sharding/">
hashed shard key
</a>
<a class="footnote-reference header-buffer" href="#footnote-populated" id="ref-populated-id1">
[
<!-- -->
1
<!-- -->
]
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use test\n\ndb.orders.createIndex( { _id: \"hashed\" } )\n\nsh.shardCollection( \"test.orders\", { _id : \"hashed\" } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">use test</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">orders</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"hashed"</span> } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">sh.<span class="lg-highlight-title lg-highlight-function">shardCollection</span>( <span class="lg-highlight-string">"test.orders"</span>, <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"hashed"</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If the following document exists in the collection, the hashed value of
the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is used to distribute the document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: ObjectId(\"5b2be413c06d924ab26ff9ca\"),\n \"item\" : \"Chocolates\",\n \"qty\" : 25\n}","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5b2be413c06d924ab26ff9ca"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"Chocolates"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">25</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To determine the hashed value of
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field used to distribute the
document across the shards, you can use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-convertShardKeyToHashed" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
convertShardKeyToHashed()
</code>
</span>
</a>
method:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"convertShardKeyToHashed( ObjectId(\"5b2be413c06d924ab26ff9ca\") )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-function">convertShardKeyToHashed</span>( <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5b2be413c06d924ab26ff9ca"</span>) )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-populated" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-populated-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
If the collection already contains data, you must create a hashed
index on the shard key before you shard the collection. For an empty
collection, MongoDB creates the index as part of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardCollection/#mongodb-method-sh.shardCollection">
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
.
</a>
</td>
</tr>
</tbody>
</table>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/views/specify-collation/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Create a View with Default Collation
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-view-with-default-collation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="create-a-view-with-default-collation">
</div>
</a>
</h1>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-manual-collation">
Collation
</a>
allows you to specify
language-specific rules for string comparison, such as rules for
letter-case and accent marks.
</p>
<p class="leafygreen-ui-1kp3ins">
This page explains how to specify a default collation for a view.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Example
<a class="headerlink leafygreen-ui-1kepx47" href="#example" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="example">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
places
</code>
collection with the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.insertMany([\n { _id: 1, category: \"café\" },\n { _id: 2, category: \"cafe\" },\n { _id: 3, category: \"cafE\" }\n])","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">places</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"café"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"cafe"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"cafE"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">])</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following operation creates a view, specifying collation at the view
level:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createView(\n \"placesView\",\n \"places\",\n [ { $project: { category: 1 } } ],\n { collation: { locale: \"fr\", strength: 1 } }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createView</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"placesView"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"places"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-number">1</span> } } ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">collation</span>: <!-- -->{ <span class="lg-highlight-attr">locale</span>: <span class="lg-highlight-string">"fr"</span>, <span class="lg-highlight-attr">strength</span>: <span class="lg-highlight-number">1</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following operation uses the view's collation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.placesView.countDocuments( { category: \"cafe\" } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">placesView</span>.<span class="lg-highlight-title lg-highlight-function">countDocuments</span>( <!-- -->{ <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"cafe"</span> } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation returns
<code class="leafygreen-ui-1l06pbn">
3
</code>
.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Collation Behavior
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You can specify a default
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
for a view at creation time. If no collation is specified, the
view's default collation is the "simple" binary comparison
collator. That is, the view does not inherit the collection's
default collation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
String comparisons on the view use the view's default collation.
An operation that attempts to change or override a view's default
collation will fail with an error.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If creating a view from another view, you cannot specify a
collation that differs from the source view's collation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If performing an aggregation that involves multiple views, such as
with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/graphLookup/#mongodb-pipeline-pipe.-graphLookup">
<code class="leafygreen-ui-1l06pbn">
$graphLookup
</code>
</a>
, the views must
have the same
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
<!-- -->
.
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/reconfigure-replica-set-with-unavailable-members/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Reconfigure a Self-Managed Replica Set with Unavailable Members
<a class="headerlink leafygreen-ui-1kepx47" href="#reconfigure-a-self-managed-replica-set-with-unavailable-members" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="reconfigure-a-self-managed-replica-set-with-unavailable-members">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#reconfigure-by-forcing-the-reconfiguration" target="_self">
<span>
Reconfigure by Forcing the Reconfiguration
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
To reconfigure a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
</a>
when a
<strong>
majority
</strong>
of
members are available, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#mongodb-method-rs.reconfig">
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
</a>
operation on
the current
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
, following the example in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#std-label-replica-set-reconfiguration-usage">
Replica Set Reconfiguration Procedure
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
This document provides steps for re-configuring a
replica set when
<em>
only
</em>
a
<strong>
minority
</strong>
of members are accessible.
</p>
<p class="leafygreen-ui-1kp3ins">
You may need to use the procedure, for example, in a
geographically distributed replica set, where
<em>
no
</em>
local group of
members can reach a majority. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-elections/#std-label-replica-set-elections">
Replica Set Elections
</a>
for more
information on this situation.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replica-set-force-reconfiguration">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Reconfigure by Forcing the Reconfiguration
<a class="headerlink leafygreen-ui-1kepx47" href="#reconfigure-by-forcing-the-reconfiguration" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="reconfigure-by-forcing-the-reconfiguration">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This procedure lets you recover while a majority of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
</a>
members are down or unreachable. You connect to any surviving member and
use the
<code class="leafygreen-ui-1l06pbn">
force
</code>
option to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#mongodb-method-rs.reconfig">
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
</a>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
force
</code>
option forces a new configuration onto the member. Use this procedure only to
recover from catastrophic interruptions. Do not use
<code class="leafygreen-ui-1l06pbn">
force
</code>
every
time you reconfigure. Also, do not use the
<code class="leafygreen-ui-1l06pbn">
force
</code>
option in any automatic
scripts and do not use
<code class="leafygreen-ui-1l06pbn">
force
</code>
when there is still a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To force reconfiguration:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Back up a surviving member.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect to a surviving member and save the current configuration.
Consider the following example commands for saving the configuration:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cfg = rs.conf()\n\nprintjson(cfg)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cfg <!-- -->= <!-- -->rs.<span class="lg-highlight-title lg-highlight-function">conf</span>(<!-- -->)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-title lg-highlight-function">printjson</span>(<!-- -->cfg)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
On the same member, remove the down and unreachable members of the
replica set from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members">
<code class="leafygreen-ui-1l06pbn">
members
</code>
</a>
array by
setting the array equal to the surviving members alone. Consider the
following example, which uses the
<code class="leafygreen-ui-1l06pbn">
cfg
</code>
variable created in the
previous step:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cfg.members = [cfg.members[0] , cfg.members[4] , cfg.members[7]]","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-1q4bxgx" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">cfg.<span class="lg-highlight-property">members</span> <!-- -->= <!-- -->[<!-- -->cfg.<span class="lg-highlight-property">members</span>[<span class="lg-highlight-number">0</span>] <!-- -->, <!-- -->cfg.<span class="lg-highlight-property">members</span>[<span class="lg-highlight-number">4</span>] <!-- -->, <!-- -->cfg.<span class="lg-highlight-property">members</span>[<span class="lg-highlight-number">7</span>]]</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
On the same member, reconfigure the set by using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#mongodb-method-rs.reconfig">
<code class="leafygreen-ui-1l06pbn">
rs.reconfig()
</code>
</a>
command with the
<code class="leafygreen-ui-1l06pbn">
force
</code>
option set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.reconfig(cfg, {force : true})","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">rs.<span class="lg-highlight-title lg-highlight-function">reconfig</span>(<!-- -->cfg<!-- -->, <!-- -->{<!-- -->force <!-- -->: <span class="lg-highlight-literal">true</span>})</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
This operation forces the secondary to use the new configuration. The
configuration is then propagated to all the surviving members listed
in the
<code class="leafygreen-ui-1l06pbn">
members
</code>
array. The replica set then elects a new primary.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
When you use
<code class="leafygreen-ui-1l06pbn">
force : true
</code>
, the version number in the replica
set configuration increases significantly, by tens or hundreds
of thousands. This is normal and designed to prevent set version
collisions if you accidentally force re-configurations on both
sides of a network partition and then the network partitioning
ends.
</p>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the failure or partition was only temporary, shut down or
decommission the removed members as soon as possible.
</p>
</li>
</ol>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/resync-replica-set-member/">
Resync a Member of a Self-Managed Replica Set
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/PlanCache.clear/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
PlanCache.clear()
<a class="headerlink leafygreen-ui-1kepx47" href="#plancache.clear--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="plancache.clear--">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#required-access" target="_self">
<span>
Required Access
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
PlanCache.clear()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-PlanCache.clear" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-method-PlanCache.clear">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Removes all cached query plans for a collection.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This page documents a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
method. This is
<em>
not
</em>
the documentation for database commands or language-specific drivers,
such as Node.js.
</p>
<p class="leafygreen-ui-1kp3ins">
For the database command, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/planCacheClear/#mongodb-dbcommand-dbcmd.planCacheClear">
<code class="leafygreen-ui-1l06pbn">
planCacheClear
</code>
</a>
command
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
For MongoDB API drivers, refer to the language-specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The method is only available from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.getPlanCache/#mongodb-method-db.collection.getPlanCache">
<code class="leafygreen-ui-1l06pbn">
plan cache object
</code>
</a>
of a specific collection; i.e.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.getPlanCache().clear()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">getPlanCache</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">clear</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For example, to clear the cache for the
<code class="leafygreen-ui-1l06pbn">
orders
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.getPlanCache().clear()","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">orders</span>.<span class="lg-highlight-title lg-highlight-function">getPlanCache</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">clear</span>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Required Access
<a class="headerlink leafygreen-ui-1kepx47" href="#required-access" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="required-access">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
On systems running with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.authorization">
<code class="leafygreen-ui-1l06pbn">
authorization
</code>
</a>
, a user must have access that
includes the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-planCacheWrite">
<code class="leafygreen-ui-1l06pbn">
planCacheWrite
</code>
</a>
action.
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.getPlanCache/#mongodb-method-db.collection.getPlanCache">
<code class="leafygreen-ui-1l06pbn">
db.collection.getPlanCache()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/PlanCache.clearPlansByQuery/#mongodb-method-PlanCache.clearPlansByQuery">
<code class="leafygreen-ui-1l06pbn">
PlanCache.clearPlansByQuery()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/killAllSessionsByPattern/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
killAllSessionsByPattern
<a class="headerlink leafygreen-ui-1kepx47" href="#killallsessionsbypattern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="killallsessionsbypattern">
</div>
</a>
</h1>
<div class="leafygreen-ui-a5yxub">
<p class="css-1cmx2d4 ef7gu8j0">
On this page
</p>
<ul class="css-1qb0mst ef7gu8j1">
<li class="leafygreen-ui-1e7a0ei">
<a class="lg-ui-0001 leafygreen-ui-e98ikf" href="#definition" target="_self">
<span>
Definition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#syntax" target="_self">
<span>
Syntax
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#command-fields" target="_self">
<span>
Command Fields
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#behavior" target="_self">
<span>
Behavior
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#access-control" target="_self">
<span>
Access Control
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Definition
<a class="headerlink leafygreen-ui-1kepx47" href="#definition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="definition">
</div>
</a>
</h2>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
killAllSessionsByPattern
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.killAllSessionsByPattern" title="Permalink to this definition">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongodb-dbcommand-dbcmd.killAllSessionsByPattern">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.killAllSessionsByPattern" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
killAllSessionsByPattern
</code>
</span>
</a>
command kills all sessions
that match any of the specified patterns.
<a class="footnote-reference header-buffer" href="#footnote-exception" id="ref-exception-id1">
[
<!-- -->
1
<!-- -->
]
</a>
</p>
</dd>
</dl>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="syntax">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n killAllSessionsByPattern: [ <pattern>, ... ]\n }\n)","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">killAllSessionsByPattern</span>: <!-- -->[ <<!-- -->pattern<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This command is available in deployments hosted in the following environments:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
: The fully
managed service for MongoDB deployments in the cloud
</p>
</li>
</ul>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command is not supported in M0, M2, and M5 clusters.
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/" target="_self">
<span>
Unsupported Commands
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
MongoDB Enterprise
</a>
: The
subscription-based, self-managed version of MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
MongoDB Community
</a>
: The
source-available, free-to-use, and self-managed version of MongoDB
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Command Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#command-fields" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="command-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The command takes an array of documents that specify the patterns to
match:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<thead class="leafygreen-ui-a9ho40">
<tr data-testid="leafygreen-ui-header-row">
<th class="leafygreen-ui-1trtg6r" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Pattern
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ lsid: { id : <UUID> } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specify the UUID portion of the session ID to kill.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ uid: <BinData> }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the hash of the owner of the sessions to kill.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ users: [ { user: <user>, db: <dbname> }, ... ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the owners of the sessions to kill. Requires
additional privileges. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-killAllSessionsByPattern-auth" target="_self">
<span>
Access Control
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ roles: [ { role: <role>, db: <dbname> }, ... ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the roles assigned to the owners of the sessions to
kill. Requires additional privileges. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-killAllSessionsByPattern-auth" target="_self">
<span>
Access Control
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
Specify an empty array to kill all sessions.
<a class="footnote-reference header-buffer" href="#footnote-exception" id="ref-exception-id2">
[
<!-- -->
1
<!-- -->
]
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To view existing sessions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/listSessions/#mongodb-pipeline-pipe.-listSessions">
<code class="leafygreen-ui-1l06pbn">
$listSessions
</code>
</a>
operation or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/listLocalSessions/#mongodb-pipeline-pipe.-listLocalSessions">
<code class="leafygreen-ui-1l06pbn">
$listLocalSessions
</code>
.
</a>
</p>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
See also:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killAllSessions/#mongodb-dbcommand-dbcmd.killAllSessions">
<code class="leafygreen-ui-1l06pbn">
killAllSessions
</code>
</a>
</p>
</div>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-exception" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<!-- -->
1
<!-- -->
]
</td>
<td class="leafygreen-ui-17r2xs0">
<em>
(
<a href="#ref-exception-id1">
1
</a>
,
<a href="#ref-exception-id2">
2
</a>
)
</em>
<!-- -->
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.killAllSessionsByPattern" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
killAllSessionsByPattern
</code>
</span>
</a>
operation ignores sessions that have
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
transactions
</a>
in prepared state.
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-killAllSessionsByPattern-behavior" target="_self">
<span>
Behavior
</span>
</a>
for details.
</td>
</tr>
</tbody>
</table>
<span class="leafygreen-ui-1qnjd6d" id="std-label-killAllSessionsByPattern-behavior">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behavior">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
In-progress Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#in-progress-operations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="in-progress-operations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Killing a session kills any in-progress operations in the session and
closes any open cursors associated with these operations.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Killed Session Availability
<a class="headerlink leafygreen-ui-1kepx47" href="#killed-session-availability" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="killed-session-availability">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The killed session may still be listed as a current session, and future
operations may use the killed session. To view existing sessions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/listSessions/#mongodb-pipeline-pipe.-listSessions">
<code class="leafygreen-ui-1l06pbn">
$listSessions
</code>
</a>
operation or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/listLocalSessions/#mongodb-pipeline-pipe.-listLocalSessions">
<code class="leafygreen-ui-1l06pbn">
$listLocalSessions
</code>
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sessions with Transactions in Prepared State
<a class="headerlink leafygreen-ui-1kepx47" href="#sessions-with-transactions-in-prepared-state" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sessions-with-transactions-in-prepared-state">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.killAllSessionsByPattern" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
killAllSessionsByPattern
</code>
</span>
</a>
operation ignores sessions that have
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/">
transactions
</a>
in prepared state. Transactions in prepared state
refer to transactions with write operations that span multiple shards
whose commit coordinator has completed the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/currentOp/#mongodb-data--currentOp.twoPhaseCommitCoordinator.action">
<code class="leafygreen-ui-1l06pbn">
"sendingPrepare"
action
</code>
.
</a>
</p>
</section>
<span class="leafygreen-ui-1qnjd6d" id="std-label-killAllSessionsByPattern-auth">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Access Control
<a class="headerlink leafygreen-ui-1kepx47" href="#access-control" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="access-control">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If the deployment enforces authentication/authorization, you must have
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-killAnySession">
<code class="leafygreen-ui-1l06pbn">
killAnySession
</code>
</a>
privilege action to run the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.killAllSessionsByPattern" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
killAllSessionsByPattern
</code>
</span>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
For patterns that include
<code class="leafygreen-ui-1l06pbn">
users
</code>
or
<code class="leafygreen-ui-1l06pbn">
roles
</code>
, you must also have
privileges that grant
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-impersonate">
<code class="leafygreen-ui-1l06pbn">
impersonate
</code>
</a>
action on the cluster
resource.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Users can kill their own sessions even without the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-killAnySession">
<code class="leafygreen-ui-1l06pbn">
killAnySession
</code>
</a>
privilege action.
</p>
</div>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#examples" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="examples">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Kill All Sessions
<a class="headerlink leafygreen-ui-1kepx47" href="#kill-all-sessions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="kill-all-sessions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following operation kills all sessions:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { killAllSessionsByPattern: [ ] } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-yzcs8i">
<pre class="leafygreen-ui-1q4bxgx" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{ <span class="lg-highlight-attr">killAllSessionsByPattern</span>: <!-- -->[ <!-- -->] } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Kill All Sessions for Specific Users
<a class="headerlink leafygreen-ui-1kepx47" href="#kill-all-sessions-for-specific-users" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="kill-all-sessions-for-specific-users">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following operation kills all sessions that have the specified
<code class="leafygreen-ui-1l06pbn">
uid
</code>
and whose owner has the specified role:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { killAllSessionsByPattern: [\n { \"uid\" : BinData(0,\"oBRA45vMY78p1tv6kChjQPTdYsnCHi/kA/fFMZTIV1o=\") },\n { roles: [ { role: \"readWrite\", db: \"test\" } ] }\n] } )","programmingLanguage":"JavaScript"}
</script>
<div class="css-16lmvu9">
<div class="leafygreen-ui-196mwvg">
<div class="leafygreen-ui-1b1bvay">
<pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">runCommand</span>( <!-- -->{ <span class="lg-highlight-attr">killAllSessionsByPattern</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"uid"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">BinData</span>(<span class="lg-highlight-number">0</span>,<span class="lg-highlight-string">"oBRA45vMY78p1tv6kChjQPTdYsnCHi/kA/fFMZTIV1o="</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">roles</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"readWrite"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"test"</span> } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
</section>
</section>
</section>
|
Subsets and Splits